C# Func Delegate
In C#, a delegate is a type that references a method. When creating a custom delegate, we follow these steps:
- Declare a delegate with a signature matching the target method.
- Create an instance of the delegate.
- Invoke the method using the delegate.
But defining custom delegates manually can be repetitive. To simplify this, C# provides a built-in delegate called Func<>, which reduces the need to declare custom delegates explicitly.
Example: Custom Delegate (Without Func)
// C# Program to demonstrate a custom delegate
using System;
class Geeks
{
// Custom delegate declaration
public delegate int Delegate(int a, int b, int c, int d);
// Method to be assigned to the delegate
public static int Multiply(int a, int b, int c, int d)
{
return a * b * c * d;
}
static void Main()
{
// Using the custom delegate
Delegate o = Multiply;
Console.WriteLine(o(12, 34, 35, 34));
}
}
Output
485520
Here, we manually define a delegate type "Delegate" that matches the method signature.
A Func delegate is a built-in generic delegate provided by C#. It simplifies delegate creation by eliminating the need for explicit delegate declarations.
Func Delegate Syntax:
// Func delegate with zero input parameters and one return type
public delegate TResult Func<out TResult>();
// Func delegate with one input parameter and one return type
public delegate TResult Func<in T, out TResult>(T arg);
// Func delegate with two input parameters and one return type
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
// Func delegate with sixteen input parameters and one return type
public delegate TResult Func<in T1, in T2, ..., in T16, out TResult>(
T1 arg1, T2 arg2, ..., T16 arg16);
- T1, T2, ... T16: Input parameters
- TResult: Return type
Example 1: Using Func Delegate
// C# Program to demonstrate Func delegate with a method
using System;
class Geeks
{
// Method assigned to the Func delegate
public static int Multiply(int a, int b, int c, int d)
{
return a * b * c * d;
}
static void Main()
{
// Using Func delegate (four input parameters, one return type)
Func<int, int, int, int, int> Delegate = Multiply;
Console.WriteLine(Delegate(10, 100, 1000, 1));
}
}
Output
1000000
Example 2: Func Delegate with One Parameter
// C# Program to demonstrate
// Func delegate with a single parameter
using System;
class Geeks
{
// Method assigned to the Func delegate
public static int DoubleValue(int num)
{
return num + num;
}
static void Main()
{
// Func delegate with one input and one return type
Func<int, int> Delegate = DoubleValue;
Console.WriteLine(Delegate(10));
}
}
Output
20
Func Delegate with Anonymous Method
Example:
// C# Program to demonstrate Func delegate with an anonymous method
using System;
class Geeks
{
static void Main()
{
// Using Func delegate with an anonymous method
Func<int, int, int> sum = delegate (int x, int y)
{
return x + y;
};
Console.WriteLine(sum(5, 10));
}
}
Output
15
Func Delegate with Lambda Expression
Example:
// C# Program to demonstrate
// Func delegate with a lambda expression
using System;
class Geeks
{
static void Main()
{
// Using Func delegate with a lambda expression
Func<int, int, int> sum = (x, y) => x + y;
Console.WriteLine(sum(5, 10));
}
}
Output
15
Important Points:
- Func<> replaces custom delegate declarations, making code more readable.
- It always returns a value, unlike Action<>, which does not return anything.
- It supports 0 to 16 input parameters and 1 return parameter.
- It works well with anonymous methods and lambda expressions.