Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

oop

According to oops fundamentels, everything has to be inside a class. Then why are we allowed to create delegates outside a class?

like image 725
vatspoo Avatar asked May 31 '11 14:05

vatspoo


People also ask

Why is a delegate declared outside 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.

Is delegate OOP?

A delegate is an object-oriented, managed, secure and type-safe function pointer in the . NET framework. A delegate signature includes its name, return type and arguments passed to it. Rather than passing data, a delegate passes a method to another method.

Can we declare delegate inside class?

You can pass methods as parameters to a delegate to allow the delegate to point to the method. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What is the delegate in c# with Example?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.


2 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?

like image 130
Eric Lippert Avatar answered Oct 23 '22 21:10

Eric Lippert


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.

like image 29
manojlds Avatar answered Oct 23 '22 20:10

manojlds