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 underlaying types of each parameters 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 examle 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

  1. class MethodCollections  
  2.     {  
  3.   
  4.         //Methods that takes parameters but returns nothing:  
  5.    
  6.         public static void PrintText()  
  7.         {  
  8.             Console.WriteLine("Text Printed with the help of Action");  
  9.         }  
  10.         public static void PrintNumbers(int start, int target)  
  11.         {  
  12.             for (int i = start; i <= target; i++)  
  13.             {  
  14.                 Console.Write(" {0}",i);  
  15.             }  
  16.             Console.WriteLine();  
  17.         }  
  18.         public static void Print(string message)  
  19.         {  
  20.             Console.WriteLine(message);  
  21.         }  
  22.   
  23.         //Methods that takes parameters and returns a value:  
  24.   
  25.         public static int Addition(int a, int b)  
  26.         {  
  27.             return a + b;  
  28.         }  
  29.   
  30.         public static string DisplayAddition(int a, int b)  
  31.         {  
  32.             return string.Format("Addition of {0} and {1} is {2}",a,b,a+b);  
  33.         }  
  34.   
  35.         public static string SHowCompleteName(string firstName, string lastName)  
  36.         {  
  37.             return string.Format("Your Name is {0} {1}",firstName,lastName);  
  38.         }  
  39.         public static int ShowNumber()  
  40.         {  
  41.             Random r = new Random();  
  42.             return r.Next();  
  43.         }  
  44.     } 

Program.cs

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Action printText = new Action(MethodCollections.PrintText);  
  6.             Action<string> print = new Action<string>(MethodCollections.Print);  
  7.             Action<intint> printNumber = new Action<intint>(MethodCollections.PrintNumbers);  
  8.   
  9.             Func<intint,int> add1 = new Func<intintint>(MethodCollections.Addition);  
  10.             Func<intintstring> add2 = new Func<intintstring>(MethodCollections.DisplayAddition);  
  11.             Func<stringstringstring> completeName = new Func<stringstringstring>(MethodCollections.SHowCompleteName);  
  12.             Func<int> random = new Func<int>(MethodCollections.ShowNumber);  
  13.   
  14.             Console.WriteLine("\n***************** Action<> Delegate Methods ***************\n");  
  15.             printText();    //Parameter: 0 , Returns: nothing  
  16.             print("Abhishek");  //Parameter: 1 , Returns: nothing  
  17.             printNumber(5, 20); //Parameter: 2 , Returns: nothing  
  18.             Console.WriteLine();  
  19.             Console.WriteLine("**************** Func<> Delegate Methods *****************\n");  
  20.             int addition = add1(2, 5);  //Parameter: 2 , Returns: int  
  21.             string addition2 = add2(5, 8);  //Parameter: 2 , Returns: string  
  22.             string name = completeName("Abhishek""Yadav");    //Parameter:2 , Returns: string  
  23.             int randomNumbers = random();   ////Parameter: 0 , Returns: int  
  24.   
  25.             Console.WriteLine("Addition: {0}",addition);  
  26.             Console.WriteLine(addition2);  
  27.             Console.WriteLine(name);  
  28.             Console.WriteLine("Random Number is: {0}",randomNumbers);  
  29.   
  30.             Console.ReadLine();  
  31.         }  
  32.     } 

Output:

Up Next
    Ebook Download
    View all
    Learn
    View all