In this article we will deal with Delegates in C#. I tried my best to keep this article very simple so that people could understand the basics of delegates.
In simple words when you invoke something on Delegates it Delegates( means hand over) to some one. Delegates acts like a bridge between a method that is invoked and a person responding.
Perhaps you can recall from high-school days that sound travels with Particles. Sound emitted at Point A(Source) reaches Point B (Receiver) with the help of medium. An example of a medium is an interface to travel sound from Point A to Point B. Similary, Delegates are interfaces to pass functionality from one object to another object. You guys may me wondering why I am teaching physics, medium was a perfect example for a Delegate.
It looks like a method Overriding but Delegates concept is much more robust and powerfull than method overriding.
A delegate in C# allows you to pass methods of one class to objects of other classes that can call those methods. You can pass method Play (method name) in Class A, wrapped in a delegate, to class B and Class B will be able to call method Play in class A.
Delegates in C# are objects which points towards a function which matches its signature. Delegates are reference type used to encapsulate a method with a specific signature.
A delegate instance encapsulates a static or an instance method. Delegates are roughly similar to function pointers in C++; however, delegates are type-safe and secure.
You can pass both static and instance methods.
A delegate represents a class.
A delegate is type-safe.
You can combine multiple delegates into a single delegate.
You can use delegates both for static and instance methods.
You can define delegates inside or outside of classes.
You can use delegates in asynchronous-style programming.
Delegates are often used in event-based programming, such as publish/subscribe.
The delegate declaration takes the form:
[attributes] [modifiers] delegate result-type identifier ([formal-parameters]);attributes (Optional)The allowed modifiers are new and the four access modifiers. result-typeThe result type, which matches the return type of the method.
identifierThe delegate name.
formal-parameters (Optional)Parameter list. If a parameter is a pointer, the delegate must be declared with the unsafe modifier. Delegates are the basis for events.
Functionality of Delegates can be accomplished in four steps.
1 Declare a delegate object with a signature that exactly matches the method signature that you are trying to encapsulate.
2. Define all the methods whose signatures match the signature of the delegate object that you have defined in step 1.
3. Create delegate object and plug in the methods that you want to encapsulate.
4. Call the encapsulated methods through the delegate object.