Action and Func Delegates in C# .NET

The Func and Action generic delegates were introduced in the .NET Framework version 3.5.

Whenever we want to use delegates in our examples or applications, typically we use the following procedure:

  • Define a custom delegate that matches the format of the method.
  • Create an instance of a delegate and point it to a method.
  • Invoke the method.

But, using these 2 Generics delegates we can simply eliminate the above procedure.

Since both the delegates are generic, you will need to specify the underlying types of each parameter as well while pointing it to a function. For for example Action<type,type,type……>

Action<>

  • The Generic Action<> delegate is defined in the System namespace of microlib.dll
  • This Action<> generic delegate; points to a method that takes up to 16 Parameters and returns void.

Func<>

  • The generic Func<> delegate is used when we want to point to a method that returns a value.
  • This delegate can point to a method that takes up to 16 Parameters and returns a value.
  • Always remember that the final parameter of Func<> is always the return value of the method. (For example, Func< int, int, string>, this version of the Func<> delegate will take 2 int parameters and returns a string value.)

Let's look at an example to see the use of both Delegates.

Create a new project named “FuncAndActionDelegates” and create a new class that holds all your methods.

MethodCollections.cs

class MethodCollections  
{  

    //Methods that takes parameters but returns nothing:  

    public static void PrintText()  
    {  
        Console.WriteLine("Text Printed with the help of Action");  
    }  
    public static void PrintNumbers(int start, int target)  
    {  
        for (int i = start; i <= target; i++)  
        {  
            Console.Write(" {0}",i);  
        }  
        Console.WriteLine();  
    }  
    public static void Print(string message)  
    {  
        Console.WriteLine(message);  
    }  

    //Methods that takes parameters and returns a value:  

    public static int Addition(int a, int b)  
    {  
        return a + b;  
    }  

    public static string DisplayAddition(int a, int b)  
    {  
        return string.Format("Addition of {0} and {1} is {2}",a,b,a+b);  
    }  

    public static string SHowCompleteName(string firstName, string lastName)  
    {  
        return string.Format("Your Name is {0} {1}",firstName,lastName);  
    }  
    public static int ShowNumber()  
    {  
        Random r = new Random();  
        return r.Next();  
    }  
} 

Program.cs​​​​​​​

class Program  
{  
    static void Main(string[] args)  
    {  
        Action printText = new Action(MethodCollections.PrintText);  
        Action<string> print = new Action<string>(MethodCollections.Print);  
        Action<int, int> printNumber = new Action<int, int>(MethodCollections.PrintNumbers);  

        Func<int, int,int> add1 = new Func<int, int, int>(MethodCollections.Addition);  
        Func<int, int, string> add2 = new Func<int, int, string>(MethodCollections.DisplayAddition);  
        Func<string, string, string> completeName = new Func<string, string, string>(MethodCollections.SHowCompleteName);  
        Func<int> random = new Func<int>(MethodCollections.ShowNumber);  

        Console.WriteLine("\n***************** Action<> Delegate Methods ***************\n");  
        printText();    //Parameter: 0 , Returns: nothing  
        print("Abhishek");  //Parameter: 1 , Returns: nothing  
        printNumber(5, 20); //Parameter: 2 , Returns: nothing  
        Console.WriteLine();  
        Console.WriteLine("**************** Func<> Delegate Methods *****************\n");  
        int addition = add1(2, 5);  //Parameter: 2 , Returns: int  
        string addition2 = add2(5, 8);  //Parameter: 2 , Returns: string  
        string name = completeName("Abhishek", "Yadav");    //Parameter:2 , Returns: string  
        int randomNumbers = random();   ////Parameter: 0 , Returns: int  

        Console.WriteLine("Addition: {0}",addition);  
        Console.WriteLine(addition2);  
        Console.WriteLine(name);  
        Console.WriteLine("Random Number is: {0}",randomNumbers);  

        Console.ReadLine();  
    }  
} 

Output

Func and Action


Similar Articles