What is Action and Func keywords in dotnet framework

0 votes
asked Jul 14, 2013 in C#.NET by Santosh

What is the purpose of using Action and Func<T> keywords in dotnet application? I have seen these keywords in Lambda expression a lot. Whenever we write simple lambda expression to work with the collections like List<T>, it is expecting the predicates and those are either Action or Func<T>. How these two are different from each other? When should I use one over the other?

Share

1 Answer

+1 vote
answered Jul 15, 2013 by Aadhira (1,213 points)
selected Jul 15, 2013 by administrator
 
Best answer

If you understand the concept of Delegates, then it is very east to understand Action and Func because, both are delegates.

If you do not know what are delegates and why should we use, then please read the below article.

http://www.pinfaq.com/122/best-explanation-for-why-use-delegate-dotnet-applications

Action is a delegate with no return value. So, if you have a scenario where you need to have a delegate without any return value then you can use Action. If you see the definition of the Action keyword, this is what you are going to see.

public delegate void Action();

There are lots of overloaded delegates we have if you want a delegate with one or more strongly type parameter.

ex: Action<T>, Action<T1, T2> .... up to 16 parameters.

If you have used ICommand in WPF application, then you must have used Action delegate to pass the ViewModel command method to CommandClass.

Lets take a simple example. We are not doing any critical functionality. We have a delegate, which is used in the method parameter for CallDelegate method. If we pass a delegate handler to that method, it just call that handler to execute. In this example I have used lambda expression to create the handler. If you run this application, it is just going to print "Hello from handler 1" and "Hello from handler 2".

public delegate void ConsoleWriteHandler();
public class ActionAndFuncDemo
{
    public void CallDelegate(ConsoleWriteHandler handler)
    {
        handler();
    }
}

static void Main(string[] args)
{
    ActionAndFuncDemo actionAndFuncDemo = new ActionAndFuncDemo();
    actionAndFuncDemo.CallDelegate(() => Console.WriteLine("Hello from handler 1"));
    actionAndFuncDemo.CallDelegate(() => Console.WriteLine("Hello from handler 2"));
    Console.Read();
}

In these kind of situation, instead creating your own delegate (ConsoleWriteHandler delegate), you could use "Action" delegate. The same can be replaced with below code.Main function is not going to change. We just remove the delegate what we created and used the one already exist in the framework. Same like this we can use Action and its overloads.

public class ActionAndFuncDemo
{
    public void CallDelegate(Action handler)
    {
        handler();
    }
}

Func is another delegate, but it will have return value. Func<> is at least with one generic type which is going to be the return value. So, it may or may not have parameter to accept, but returns a value.

When you use Func<> delegate, always the last generic type is return type. Same as Action delegate, in Func<> also we have overloaded delegates upto 16 parameter and one return value. If you see the definition of the Func<T> keyword, this is what you are going to see.

public delegate TResult Func<out TResult>();

In Func<int>, delegate does not accept any parameter but the return type is integer.
In Func<int, string>, delegate accept one integer parameter and return string value.

If you use Func<string> in the previous example, we can modify the same example code like below

static void Main(string[] args)
{
    ActionAndFuncDemo actionAndFuncDemo = new ActionAndFuncDemo();
    actionAndFuncDemo.CallDelegate(() => "Hello from handler 1");
    actionAndFuncDemo.CallDelegate(() => "Hello from handler 2");
    Console.Read();
}

public class ActionAndFuncDemo
{
    public void CallDelegate(Function<string> handler)
    {
        var result = handler();
        Console.WriteLine(result);
    }
}

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...