Saturday, 9 May 2015

Delegate, Func and Anonymous Method in C#

Most confusing term, if you search these things on internet you will get lot of material related to this. Instead I thought to write this blog because I believe this can be explained in a very simple way. Let me give a try.

we will talk about all these three terms one by one.

1. Delegate: A delegate is a function pointer. In simple words, it keeps the reference of a function just like c and c++ (if you aware of these languages). Use of delegate in c# is callback.

Declaration syntax for delegate is same as a function in c# with an addition of 'delegate' keyword.
Ex:public delegate decimal CalculateInterestDelegate(decimal amount);

what this declaration means: This declaration means, CalculateInterestDelegate is a delegate which can hold an reference of function, whose return type is decimal and takes one parameter of decimal type.

Here I created a class and defined a delegate within that. I also defined a method which takes a delegate parameter which says that send me the delegate (function) and I'll invoke it. Now let's see how to use this delegate:public class FuncDelegate { static decimal CalculateInterest(decimal amount) { decimal intRate = 0.1M; return amount * intRate; } static void Main(string[] args) { decimal calculatedAmount = FuncDelegateExample.GetCalculatedAmount(5000, CalculateInterest)); } }

Above class has a method called "CalculateInterest" which takes decimal parameter and returns a decimal. So this method can be assigned to the delegate which we can define like this:CalculateInterestDelegate del = new CalculateInterestDelegate(CalculateInterest);

A delegate can hold more than one function pointer at a time and execute all of them together. This is how we can add another methods to the above delegate del += CalculateInterest2; del += CalculateInterest3; (say 'CalculateInterest2' & 'CalculateInterest3' are the another methods of same signature.)
Now when we say del(5000); all three methods assigned to this delegate will be called.
Any point of time if we want to remove any of the method reference from delegate, we can do it like this:del -= CalculateInterest3;

Ok, Now let's come to the example which we have given here (FuncDelegate class). In this class we are passing the function "CalculateInterest" as parameter, it means we are assigning the "CalculateInterest" function to the "CalculateInterestDelegate" delegate and asking to "FuncDelegateExample" to callback the method which is passed as delegate pointer. Hence in other words, we can also say that Delegate is used to communicate between two parties (classes).

I gave this example because with this example I also want to show you, How a method can be created which takes a delegate as a parameter and also down here I'll explain you the Func and Anonymous concept using same example.

Hope you understand the Delegate. Now let's talk about Func.

2. Func: In simple it's generic delegate. Syntax for Func is Func<T, TResult>. There are multiple overloaded types of Func.

Since Func is a generic delegate so we can change our "GetCalculatedAmount" method of "FuncDelegateExample" class like this:public class FuncDelegateExample{public static decimal GetCalculatedAmount(decimal amount, Func<decimal, decimal> funcDel) { return amount + funcDel(amount); }}
In this case, there is no need to define the "CalculateInterestDelegate" delegate.

3. Anonymous Method: It is a method without a method name :). In other way, It's an "inline" statement or expression that can be used wherever a delegate type is expected. We can use it to initialize a named delegate or pass it instead of a named delegate type as a method parameter.

At last, Just for info: We can consume "FuncDelegateExample.GetCalculatedAmount" method using lamda expression like this:decimal calcAmount = FuncDelegateExample.GetCalculatedAmount(5000, (x => x* 0.1M));
In this case we don't need "CalculateInterest" method in "FuncDelegate" class.

I am Done. Hope this was helpful for you.

Thank You for reading. Don't forget to like it and feel free to give your feedback/comment.