English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Func delegate

C# includes built-in generic delegate types Func and Action, so in most cases you do not need to manually define custom delegates.

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out Parameters. The last parameter is considered as the out parameter.

What is the Func delegate

The Func delegate represents a delegate with a return type.

The Func in the System namespace defines an in parameter and an out parameter The delegate, as shown below:

namespace System
{    
    public delegate TResult Func<in T, out TResult>(T arg);
}

The last parameter in the angle brackets <> is considered as the return type, and the other parameters are considered as input parameter types, as shown in the figure below.

Func delegate

The Func delegate with two input parameters and one output parameter will be as follows.

Func delegate

The following Func delegate accepts two int type input parameters and returns an int type value:

Func<int, int, int> sum;

You can assign any method to the above func delegate that requires twointparameters and return aintvalue.

class Program
{
    static int Sum(int x, int y)
    {
        return x + y;
    }
    static void Main(string[] args)
    {
        Func<int, int, int> add = Sum;
        int result = add(10, 10);
        Console.WriteLine(result); 
    }
}
Output:
20

The Func delegate type can include from 0 to16Different types of input parameters. However, it must include an out parameter for the result. For example, the following Func delegate has no input parameters and only contains out parameters.

Func<int> getRandomNumber;

Func Delegate Example Explanation

  • Func has at least 0 input parameters, up to16An input parameter, and the generic return value according to the return value. There must be a return value, not void.

  • Func<int> indicates there are no input parameters, and the return value is a delegate of int type.

  • Func<object, string, int> indicates the input parameters are object, string, and the return value is a delegate of int type.

  • Func<object, string, int> indicates the input parameters are object, string, and the return value is a delegate of int type.

  • Func<T1, T2, T3, int> indicates the input parameter is T1, T2, T3(Generic), a delegate that returns int type.

C# Func Delegate and Anonymous Method

You can use the delegate keyword to assign an anonymous method to a Func delegate.

Func<int> getRandomNumber = delegate()
                            {
                                Random rnd = new Random();
                                return rnd.Next(1, 100);
                            };

C# Func Delegate and Lambda

Func delegate can also be used with lambda expressions, as shown below:

Func<int> getRandomNumber = () => new Random().Next()1, 100);
//or
Func<int, int, int> Sum = (x, y) => x + y;

 Key Points to Remember

  1. Func is a built-in delegate type.

  2. The Func delegate type must return a value.

  3. The Func delegate type can have zero to16one input parameter.

  4. The Func delegate does not allow ref and out parameters.

  5. The Func delegate type can be used with anonymous methods or lambda expressions.