We used delegate a lot in some or other way. Like as we know, the event base model that we use in windows as web programming, is based on delegate only. I am in the habit of using delegates often. In last few days, I found a very elegent way to use the delegate.I was in the habit of of using only generic delegates that allows us type safety and lot more flexibility.
In our traditional programming, we use the delegate as in the following steps.
- Declaring the delegate
- Creating object of the delegate and assigning it to appropriate function
- Calling the delegate
Lets see the code
public class Program { //Declaring an delegate public delegate void MyDelegate<R, S>(R r, S s); public static void Main(string[] args) { //Creating an Action variable and assigning it to a function MyDelegate<int, int> m1 = Multiply; //Calling the method m1(3, 4); Console.ReadKey(); } public static void Multiply(int i, int j) { Console.WriteLine(i * j); } }
.NET 2.0 introcuced one generic delegate Action which takes a single parameter and returns nothing. Its declaration is
–Public delegate void Action<T1>(T1 t1) // Takes 1 parameter and returns nothing
This is a very much elegatent way to use the delegate.
And C# 3.0, introduced 4 delegates which are as
- Public delegate void Action() // Takes no parameter and returns nothing
- Public delegate void Action<T1,T2>(T1 t1,T2 t2) // Takes 2 parameters and returns nothing
- Public delegate void Action<T1,T2,T3>(T1 t1,T2 t2,T3 t3) // Takes 3 parameters and returns nothing
- Public delegate void Action<T1,T2,T3,T4>(T1 t1,T2 t2,T3 t3),T4 t4) // Takes 4 parameters and returns nothing
Lets see them running
public class Program { public static void Main(string[] args) { //Creating an Action variable and assigning it to a function Action myAction = Multiply; //Calling the method myAction(3, 4); //Also can be called as myAction.Invoke(3, 4); } public static void Multiply(int i,int j) { Console.WriteLine(i * j); } }
As from the code, we can also see that Action delegate also provide a method invoke to call the method.
These Action delegate also can be used with Anonymous Methods as
public class Program { public static void Main(string[] args) { //Creating an Action variable and assigning it to a function Action myAction = delegate(int i, int j) { Console.WriteLine(i * j); }; //Calling the method myAction(3, 4); Console.ReadKey(); } }
Also can be used with lambda function. Lets see
public class Program { public static void Main(string[] args) { //Creating an Action variable and assigning it to a function Action<int> myAction = s => Console.WriteLine(s * 5); //Calling the method myAction(3); Console.ReadKey(); } }
So code delegently and efficiently.
Note: The ForEach and ForEach<T> methods each take an Action<T> delegate as a parameter. The method encapsulated by the delegate allows us to perform an action on each element in the array or list.
Happy .Neting….
Cheers,
Brij
Hi
Thanks so much for this great intro into actions , I was having a bit of a problem understanding them.
Thanks Peter!! Glad to know that it helped you !!