Delegates in C#
Let's start with a very basic definition of Delegates in the real world. Delegates in the real world refer to transferring the work/tasks assigned to self to someone else. A team leader, supervisor, the manager can delegate work to his subordinate.
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
When to use Delegate :
Source: https://msdn.microsoft.com/en-us/library/ms173173.aspx
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
When to use Delegate :
Source: https://msdn.microsoft.com/en-us/library/ms173173.aspx
Use a delegate in the following circumstances:
- An eventing design pattern is used.
- It is desirable to encapsulate a static method.
- The caller has no need to access other properties, methods, or interfaces on the object implementing the method.
- An easy composition is desired.
- A class may need more than one implementation of the method.
Syntax:
<access modifier> delegate <return type>
<delegate_name>(<parameters>)Example:
public delegate void calculate (int value);
Comments
Post a Comment