What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21642 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Func and Action Delegates

Func and Action Delegates

1 vote(s)
Rating: 5 out of 5
Article posted by Gopesh9 on 8/22/2012 | Views: 2291 | Category: C# | Level: Intermediate | Points: 250 red flag


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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About G. S.

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, August 03, 2012
Level:Starter
Status: [Member]
Biography:A Dot Net Learner. Currently working in Software Company.
>> Write Response - Respond to this post and get points
Related Posts

We will use advanced features such as attributes, threads, and exception handling.

In this small article I am going to explain how to access a method of .aspx page into an UserControl.

In this quick post we will learn all about destructors and its all properties.

Definition of Overrtide for Beginners with the help of a example.

Detect or Start a processing of a file only after it has copied to you system.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 8:49:26 PM