![]() |
VOOZH | about |
In C#, Func is a built-in generic delegate type that represents a method returning a value. It simplifies working with delegates by removing the need to explicitly declare custom delegate types.
Syntax:
Func<T1, T2, ..., TResult> variableName = method_or_lambda;
T1, T2, ... : Input parameter types.TResult: Return type of the method.485520
Here, we manually define a delegate type "Delegate" that matches the method signature.
Output:
36
Here, Func<int, int> takes one integer parameter and returns an integer result.
Output:
12
This Func<int, int, int> takes two integers and returns their sum.
When a method has no input but returns a value
Output:
Hello, World!
If logic needs multiple lines, use braces {}.
Output:
20
You can also point a Func delegate to a named method.
Output:
27
Func is commonly used in LINQ queries.
Output:
2
4
6