This article demonstrates how to create Delegate and Anonymous function. Also this article explains the evolution of delegates in C#.
Introduction
In this article we will see what is Delegate. How to create Delegate and Named Method. What are Anonymous Function and its types. How to use Anonymous method instead of Named method. What is Lambda Expression. How Lambda Expression simplify the coding part.
Also we will see where we are using the Delegate in real life more often.Delegate
Delegate in C# is equal to C++ functional pointer but its type safe. By using delegate we can pass method as parameter. Delegate is a type that represents reference to a method that matches parameters and return types. Later part of this article We will see how to create delegate and Named Methods. Before C# 2.0, only way of declaring the Delegate is by using Named method.
Anonymous Functions
Anonymous function is an inline statement or expression that can be used wherever delegate type is expected. There are two kinds of Anonymous Functions.
- Anonymous Method
- Lambda Expression
Anonymous Method
In C# 2.0 and above we can create delegate using Anonymous method. By using Anonymous method we can pass inline statement or expression to the delegate type as a parameter.
Lambda Expression
Lambda expression is introduced in C# 3.0. Before that we are only create delegate either by using Named method or Anonymous method.
Delegate Creation
We can create the delegate by using the delegate keyword. Below delegate accept two integer parameters and return integer value. However we can create delegate without return any value by using void.
delegate int Add(int a, int b);
Named Method
Named method is nothing but the method representation of the delegate type. Below method represent the Add delegate. Because it accepts two integer parameter and returns integer value.
public int Calculator(int first, int second)
{
return first + second;
}
Now we need to pass the Named method to the delegate instantiate as a parameter.
Add add=new Add(Calculator)
Next we need to execute the delegate object by passing values to the Named methods.
int result = add(5,10);
Using Anonymous Method instead of Named Method
By using Anonymous method we must ensure that the project or website is created by using .NET Framework 2.0 or above.
delegate int Add(int a, int b);
Creating separate method (Named method) and passing the method to delegate instantiate is little bit tedious one. To overcome this problem, C# enables to pass statement or expression during delegate instantiate whenever delegate type will be called.
Add add=delegate(int first,int second)
{
return first + second;
}
Finally we need to pass the values to anonymous methods.
int result = add(5,10);
Using Lambda Expression
Lambda expression is also an Anonymous method which is introduced in .NET Framework 3.0. It is very useful when we are writing LINQ Queries.
delete int Add(int a, int b)
Next step, we are using Lambda expression to instantiate the Add delegate.
Add add=(int first, int second) => first + second;
Execute the delegate by passing values.
int result=add(5,10);
Real Scenario
We are using Delegate very often in real scenario. Most of the developers are using button in ASP.NET Page. When we add a new button in aspx page and double click the button we are navigate to the code behind file. In code behind file Asp.net generate some code for us also it hides some code in partial class. People who were worked on ASP.NET 1.0 may be aware.
Button.Click+=new EventHandler(Button_Click)
protected void Button_Click(object sender, EventArgs e)
{
}
public delegate void EventHandler(object sender, EventArgs e)
Here EventHandler is the delegate. Click is the Event. Button_Click is the Name Method. So, when we click the button the delegate EventHandler called the Button_Click method. Also it is possible by using delegate we can execute more than one method.
Evolution of Delegate
In C# 1.0 we are depends on Named method to create delegate. After introduced Anonymous function in C# 2.0 we are passing expression to the delegate type. In C# 3.0 Lambda Expression makes the developer life much easier than the previous. Still we can use Named Methods are anonymous functions in latest framework also.
Conclusion
Thanks for reading this article. Hope this article gives some idea about how to create delegate, named methods and Anonymous methods.
Reference
http://msdn.microsoft.com/en-us/library/ms173171.aspx
http://msdn.microsoft.com/en-us/library/bb882516.aspx