Func and Action Delegates

Gopesh9
Posted by in C# category on for Intermediate level | Points: 250 | Views : 5826 red flag
Rating: 5 out of 5  
 1 vote(s)

The Func and Action Delegates were introduced in .Net Framework of 3.5. Now using this System Defined delegates, you don't have to write any other Delegate.

Func And Action Delegates

The .Net Framework 3.5 has introduced two sets of System-Defined Delegates named Func and Action. Action<> and Func<> are extremely useful tools for reducing duplication in code and decreasing coupling. The Func Delegate encapsulates a method that accepts between zero and four arguments and returns a value. The Action Delegate encapsulates a method that accepts between zero to four parameters and does not return a value i.e. Action delegates differ from Func in that the method must returns void.

The importance of these two System-Defined delegates is to reduce the number of delegates you define explicitly. Now you all will think that why these two delegates were introduced in 3.5, they can be introduced earlier also. The answer of your query is that the reason of introduction of Func and Action is their relationship with Lambda Expressions. Now you must know that every lambda expression's underlying type is one of these generic delegates. This means we can pass lambda expression directly to the method without creating the delegate explicitly. Many of the LINQ standard query operators accept Func arguments.

Func delegate

There are mainly five variations upon the Func Delegate, each allowing different number of parameters to be represented. It must be noted that the last element of all the lists are the return type of Func delegate.

public delegate TResult Func<TResult>()
public delegate TResult Func<T, TResult>(T arg)
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2)
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3)
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)

public delegate TResult Func<TResult>()

This is the basic variant of Func delegate representing methods that return a value but have no parameters.

Exapmle:

Func<int> value = delegate { return 5; };
Console.WriteLine("{0}", value());   // Outputs "5"

public delegate TResult Func<T, TResult>(T arg)

This delegate represents methods that return a value and have only one parameters.

Exapmle:

Func<int,int> value = delegate(int a) { return a; };
Console.WriteLine("{0}", value(6));   // Outputs "6"

public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2)

This delegate represents methods that return a value and have two parameters.

Exapmle:

Func<int,int,int> value = delegate(int a,int b) { return a*b; };
Console.WriteLine("{0}", value(5,6));   // Outputs "30"

public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3)

This delegate represents methods that return a value and have three parameters.

Exapmle:

Func<int,int,int,long> value = delegate(int a,int b,int c) { return a*b*c; };
Console.WriteLine("{0}", value(5,6,4));   // Outputs "120"

public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)

This delegate represents methods that return a value and have four parameters.

Exapmle:

Func<int,int,int,int,string> value = delegate(int a,int b,int c,int d) { return "The method have four Parameters"; };
Console.WriteLine("{0}", value(5,6,4,3));   // Outputs "The method have four parameters"

Action delegate

Like Func, Action delegate has also five variations. These allow the encapsulation of methods that have up to four parameters but do not return a value. The five signatures are as follows.

public delegate void Action()
public delegate void Action<T>(T arg)
public delegate void Action<T1, T2>(T1 arg1, T2 arg2)
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)

public delegate void Action()

This is the basic variant of Action delegate representing methods that does not a value and have no parameters.

Exapmle:

Action message = delegate { Console.WriteLine("Hello"); };
message(); //Outputs "Hello"

public delegate void Action<T>(T arg)

This delegate represents methods that does not return a value and have only one parameters.

Exapmle:

Action<int> message = delegate(int a) { Console.WriteLine("The value is " + a); };
message(5);   // Outputs "The value is 5"

public delegate void Action<T1, T2>(T1 arg1, T2 arg2)

This delegate represents methods that does not return a value and have two parameters.

Exapmle:

Action<int,int> message = delegate(int a,int b) { Console.WriteLine("The addition of two number is " + (a+b)); };
message(5,6);   // Outputs "The addition of two number is 11"

public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)

This delegate represents methods that does not return a value and have three parameters.

Exapmle:

Action<int,int,int> message = delegate(int a,int b,int c) { Console.WriteLine("The addition of three number is " + (a+b+c)); };
message(5,6,7);   // Outputs "The addition of two number is 18"

public delegate TResult Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)

This delegate represents methods that does not return a value and have four parameters.

Exapmle:

Action<int,int,int,int> message = delegate(int a,int b,int c,int d) { Console.WriteLine("The addition of four number is " + (a+b+c+d)); };
message(5,6,7,2);   // Outputs "The addition of two number is 20"

Conclusion

In this article I have used anonymous methods to specify the values of delegates, you can use lambda expression also i.e. lambda syntax can be used to create either a Func or an Action. Just keep in mind from now onward dont create the delegates explicitly, you can just call the System Defined Delegates.

Page copy protected against web site content infringement by Copyscape

About the Author

Gopesh9
Full Name: G. S.
Member Level:
Member Status: Member
Member Since: 8/3/2012 2:27:02 AM
Country: India
G. S. .Net Developer
http://www.dotnetfunda.com
A Dot Net Learner. Currently working in Software Company.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)