According to oops fundamentels, everything has to be inside a class. Then why are we allowed to create delegates outside a class?
-
@Mehrdad - think he just meant OOP. – JonH May 31 '11 at 14:44
5 Answers
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?

- 647,829
- 179
- 1,238
- 2,067
-
@Eric- How can you say the idea of class is not fundamental to programming? Every book and website mentions that classes are fundamental to oop. – vatspoo Jun 02 '11 at 12:07
-
5@vatspoo: Could you invent an object oriented language that didn't have classes? Could you program in an object-oriented style in a language that doesn't have classes? Yes, and yes. It is perfectly possible to program in an OO style in Scheme or JavaScript or C, despite the fact that none of those have classes. – Eric Lippert Jun 07 '11 at 06:06
-
@Oscar: That's too broad a question for 600 characters. But anything you can do in C++ you can do in C, just a bit uglier. For example, suppose I wanted to have the concept of "all objects are convertible to a base object type" in C. COM objects have that concept in the form of the IUnknown interface, and there are well-established ways of talking to IUnknown in C. Remember, COM is a binary standard for objects; all it says is how the thing has to be laid out in memory, not what the programming language text that calls the code looks like. – Eric Lippert Jun 16 '11 at 22:57
-
@vatspoo: Just as a quick exception: Javascript is prototype-based. It's still OOP, but classes never enter the mix. (In fact, it's the other "big" kind of OOP) – Mauricio Jun 24 '11 at 02:43
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.

- 290,304
- 63
- 469
- 417
-
In instance, we can mention that a delegate isn't of a class. Maybe this is a confusion of question's author and the reason of asking it. It's like a class declaration. – Matías Fidemraizer May 31 '11 at 14:45
-
-
1@vatspoo - when you are declaring a delegate outside a class, you are actually creating a new type / class. You create an instance of the array. You are not really creating a new type of array are you? – manojlds May 31 '11 at 14:48
-
@manojlds- and about structures? they are value types and we can declare them outside the class. – vatspoo Jun 02 '11 at 11:24
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.

- 1
- 1

- 205,094
- 128
- 528
- 886
-
1Thank you...saved me the typing and blood pressure hit! My favorite real-world example of this, btw, is the GTK+/GNOME code on Linux. Totally OO programming style, done in C. – Harper Shelby May 31 '11 at 14:47
-
@Harper: Same with Windows! Windows are objects that pass around messages/arguments. :) – user541686 May 31 '11 at 14:58
C# is a multi-paradigm language, not a pure object-oriented language.

- 2,994
- 1
- 16
- 14
-
1
-
1well if the question is "why are we allowed to create delegates outside a class in C#, despite this violating OOP (assuming it does)?" what i'm saying is "Because C# allows it, C# isn't necessarily pure OOP" – τεκ May 31 '11 at 18:36
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.

- 12,117
- 26
- 122
- 206