C# Func Delegate

In c#, Func is a built-in generic delegate. It is useful to hold the reference of one or more methods with the same method signature without declaring any custom delegate object.

 

In previous chapters, we learned about delegates and will be used as shown following to hold the reference of methods with the same signature.

 

using System;

namespace TutlaneExamples
{
   // Declare Delegate
   public delegate int SampleDelegate(int a, int b);
   class Program
   {
      static void Main(string[] args)
      {
         int result = 0;
         SampleDelegate dlgt = Add;
         result = dlgt(10, 90);
         Console.WriteLine("Add Result: {0}", result);
         dlgt = Subtract;
         result = dlgt(10, 90);
         Console.WriteLine("Subtract Result: {0}", result);
         Console.ReadLine();
      }
      public static int Add(int a, int b)
      {
         return a + b;
      }
      public static int Subtract(int x, int y)
      {
         return x - y;
      }
   }
}

If you observe the above example, we created a delegate object called “SampleDelegate” to hold the reference of Add & Subtract methods.

 

When we execute the above example, we will get the result as shown below.

 

Add Result: 100
Subtract Result: -80

To avoid the declaration of custom delegate object (SampleDelegate), as we defined in the above example, the generic built-in delegates such as Func, Action, Predicate have been introduced in C# 3.0.

 

C# 3.0 onwards, the Func delegate will be available automatically with the System namespace, and it will accept zero or more (16) input parameters and one output parameter.

C# Func Syntax

Following is the syntax of declaring a Func delegate with one input parameter and one output parameter in c#.

 

public delegate TResult Func<in T, out TResult>(T arg);

Here, the last parameter in the angle bracket < > will be considered as an output parameter (return type), and the remaining all are considered as input parameters. 

 

ParameterDescription
TResult It represents the type of return value of the method that the delegate encapsulates.
T It represents the type of parameter of the method that the delegate encapsulates.

If we want to create a Func delegate with two input parameters and return type (output parameter), that would be as shown below.

 

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

Like this, a Func delegate can include 0 to 16 input parameters of different types and one output parameter for the result. 

C# Func Example

Following is the example of defining the Func delegate to hold the reference of one or more methods which is having the same method signature.

 

using System;

namespace TutlaneExamples
{
   class Program
   {
      static void Main(string[] args)
      {
         int result = 0;
         Func<int, int, int> dlgt = Add;
         result = dlgt(10, 90);
         Console.WriteLine("Add Result: {0}", result);
         dlgt = Subtract;
         result = dlgt(10, 90);
         Console.WriteLine("Subtract Result: {0}", result);
         Console.ReadLine();
      }
      public static int Add(int a, int b)
      {
         return a + b;
      }
      public static int Subtract(int x, int y)
      {
         return x - y;
      }
   }
}

If you observe the above example, we created a Func delegate object (dlgt) with two input parameters (int) and one output parameter (int) to return an int value.

 

Here, the declaration of Func<int, int, int> dlgt is same as the SampleDelegate object in the previous example.

 

When we execute the above example, we will get the result as shown below.

 

Add Result: 100
Subtract Result: -80

Every time while creating the Func delegate, we must need to remember that we can include 0 to 16 input parameters of different types, and that is optional, but we must need to include one output parameter for the return type.

 

Following is the example of creating a Func delegate with zero (0) input parameters and one output parameter.

 

Func<int> dlgt

C# Func with Anonymous Method

In c#, we can assign the anonymous method directly to the Func delegate by using the delegate keyword like as shown below.

 

static void Main(string[] args)
{
    Func<int, int, int> dlgt = delegate(int x, int y)
    {
       return x + y;
    };
    int i = dlgt(10, 90);
    Console.WriteLine("Result: {0}", i); // Result: 100
}

If you observe the above code, we assigned an anonymous method directly to the Func delegate object (dlgt) using the delegate keyword.

C# Func with Lambda Expressions

In c#, we can also use a Func delegate with lambda expressions. The lambda expressions are the shorthand way of declaring the anonymous method.

 

static void Main(string[] args)
{
   Func<int, int, int> dlgt = (x, y) => x + y;
   int i = dlgt(10, 90);
   Console.WriteLine("Result: {0}", i); // Result: 100
}

C# Func Overview

Following are the important points which we need to remember about Func delegate in c#.

 

  • In c#, Func is a built-in generic delegate, and it is useful to hold the reference of one or more methods which is having the same method signature.
  • Func delegate is introduced in C# 3.0, and it is available with the System namespace.
  • While creating a Func delegate, we can include 0 to 16 input parameters of different types and one output parameter for the result.
  • While creating a Func delegate, specifying input parameters is optional, but we must need to include one output parameter for the return type.
  • We can use Func delegate in anonymous methods and Lambda expressions.