User Id :    Password :      New Member   Forgot Password  
 
Delegate and Action and Func and lamda
Description 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.   No. of Views     1745
  Rating     0
Author Pranay Rana   Posted On     29 Aug 2012
Tags C#,C# : How To    

Sample Code   Download Code

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#.

About Author

About Author Hey, I am Pranay Rana, working as a Senior Software engineer. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5 years now.
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
You can visit me on my blog - http://pranayamr.blogspot.com/
Pranay Rana
No Photo
 
Country India
Company GMind Solutions
Home Page http://pranayamr.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code