开发者

why can we declare delegates outside a class? Is it not against OOP concept?

According to oops fundamentels, everything has to be inside a class. Then why are we allowed to create delegates开发者_如何学Python outside a class?


First off, your idea of what is an OOP "fundamental" is not a fundamental. The inventor of the term "object oriented", Alan Kay, famously said:

I made up the term 'object-oriented', and I can tell you I didn't have C++ in mind.

Certainly the idea that lexically "everything goes inside a class" is not a fundamental principle of OOP. It's not even clear to me that the idea of a "class" is fundamental to object-oriented programming; the "class-based inheritance" pattern is just one way of embedding into a language support for concepts such as message passing, data abstraction and implementation sharing.

Second, your implication that C# the language designers are trying to make a language that conforms to "fundamentals" of OOP is putting the cart before the horse. Rather, we want to make a language that supports large, diverse teams working together on versionable, independent, interacting software components on our platforms. OOP just happens to be a great way to do that, so that's what we're doing.

We are certainly not trying to make a "pure" OOP language by any means. We'll take ideas from any paradigm if they support real-world customer-benefitting scenarios. There are ideas in C# taken from OOP, from procedural programming, from functional programming, from dynamic languages, and so on.

Third, your question is logically inconsistent. You ask why you can define a delegate outside a class. But a delegate is a class. A delegate is a very special class; it is always sealed, it always inherits from System.MulticastDelegate, and it always has the same members. But it is a class. We give it special syntax to call out that it is a very special kind of class. It makes sense to be able to define a delegate class outside of a class.

Fourth, the ultimate reason why it is legal to put a delegate outside a class is because it is highly convenient to do so. What is the class that you think Func<T> or EventHandler should go inside? Would that class be "OOP"? According to conventional "OOP" wisdom, a class should represent a concept by associating operations with data; what conceptual class of things does your proposed parent class of Func<T> represent, and what are the operations and data on it?

There is no such sensible outer class, and there are no operations or data associated with the "outer class" of Func<T>. So why force the user to define a useless outer class, just to be conformant with someone's mistaken idea of what "OOP" means?


Actually Delegates are a type ( class). It is just a syntactic sugar, if you may, when you declare a delegate type

public delegate int PerformCalculation(int x, int y);

A delegate is a type that safely encapsulates a method. Delegate types are derived from the Delegate class in the .NET Framework.

http://msdn.microsoft.com/en-us/library/ms173172%28v=vs.80%29.aspx

So when you are declaring a delegate outside a class, you are actually creating a new type / class. You then create a delegate instance within a class.

With that said, like @Mehrdad excellently points out, everything has to be inside a class is not a requirement for OOP.


That's NOT an OOP "fundamental".

It just happens to be a language choice in some languages like Java.

Object-oriented programming simply means to model a problem using entities called "objects", which have state and behavior. It says nothing about where your code should go.

In fact, you can make "objects" without even having classes in the first place. Just return a delegate that has a closure, and you have an object.

Example:

//An "adder" that adds the value you give it to its current value
Converter<int, int> MakeAdder(int addend) //"constructor"
{
    return msg =>
    {
        addend += msg;
        return addend;
    };
}

//...
var adder = MakeAdder(100); //Now I have an adder object!
for (int i = 0; i < 10; i++)
    Console.WriteLine(adder(i));

It's completely beside the point that a lambda in C# is a class. You can do something like this in a language like Scheme, where there's no such thing as a "class" or an "object" that exists fundamentally, only lambdas.


C# is a multi-paradigm language, not a pure object-oriented language.


I've also wondered this before but then I understood the reason for its being declared directly inside the namespace.It's because when you declare a delegate a huge class for that delegate is created in the backgound.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜