Pranay Rana: Delegate and Action and Func and lamda

Saturday, August 18, 2012

Delegate and Action and Func and lamda

This post is about the new delegate types included in .net framework which are Action and Function. How to use this type and what is difference between this two type. Also to resolve confusion between Action, Function, Delegate and Lamda.

Action- This type Points to the function which return void i.e Encapsulate method which is returning void. Action is of Delegate type so its similar to delegate which is pointing to method void. Action is type which make code simple and hide the implementation of delegate.

Syntax of Action Type or Function
>public delegate void Action()
other variation check on msdn : Action

Example of Action
public class ActionDemo
{
     public void ActFunction(int a)
     {
     }
     public void ActFunction1()
     {
     }

     static void Main()
     {
        ActionDemo ad = new ActionDemo();
            

        Action act1 = new Action(ad.ActFunction1);
        act1();     
        Action>int< act = new Action>int<(ad.ActFunction);
        act();
     }
}

Func -This type to point the function which has return value.i.e Encapsulate method which is returning value. Func is of Delegate type so its similar to delegate which is pointing to method which returns value. Func is type which make code simple and hide the implementation of delegat which points to method which return value.
Syntax of Func
>public delegate TResult Func>in T, out TResult<(T arg)
other variation check on MSDN  : Func

Example of Func
public class FuncDemo
{
     public int FuncCall(int a)
     {
            return 0;
     }
     public int FuncCall1()
     {
            return 0;
     }

     static void Main()
     {
        FuncDemo fd = new FuncDemo();

        Func>int< func = new Func>int<(fd.FuncCall);
        func();
        Func>int,int< func1 = new Func>int,int<(fd.FuncCall);
        func1(2);
     }
}
Func Vs Action
Both are Delegate type. Difference between Func and Action is - Func can point to the function which must have return value, Action can point to the function which must have return type void.

Delegate vs Func , Action
 - Both Action and Function are of Delegate type, so both of this type can able to perform same function which can be perform by Delegate.
 - Delegate Can point to function which return value and not return value.
Read more about Deletegate : Delegate

Confusion between Lamda and Delegate, Action, Func
Lamda - Replacement of anonymous function i.e allows to create anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
Action>string< act2 = n => { string s = n + " " + "World"; Console.WriteLine(s); };
act2("abc");

Func>string, string< fun = (n1) => { return n1; };
fun("pranay");

del myDelegate = x => x * x;
    int j = myDelegate(5); 
Conclusion
So by this post you can able to figure out the difference between the types of C#.

1 comment: