Sunday, April 7, 2013

delegates in .net

What is delegate ?

Delegates are a function pointer which points to single or multiple methods. It behaves the same as the method to which it points.

Advantages of using Delegates:

1. Delegates are type safe objects.
2. It can helps us to accelerate simple reuse of code.
3. Can furnish an extraordinary measure of adaptability in your outlines.
4. Delegates are also useful during asynchronous programming.

Example:
Here i will show you how delegate is declared and used.

In Below image i have declared delegate called add and in the main class i have created the object for delegate pointing to method addmethod in class addmain.


Now in the void main method after creating this delegate object, we can see that its starts to behave like the method to which it points. hence we can pass the number of parameters same as to the parameters in method to which it points.

After Executing the project we get our desired result.



No comments:

Post a Comment