Func
is a delegate type in C# that represents a method that has no input parameters or arguments, but returns a value of a specified type. It is part of the System
namespace in .NET Framework.
The Func
delegate type can be used to create a delegate that represents a method with a return type and zero or more input parameters. The Func
delegate type has many overloads, each with a different number of input parameters and return type.
Here is an example of how to use the Func
delegate type to create a delegate that represents a method that takes two integer input parameters and returns their sum:
csharpFunc<int, int, int> sumDelegate = (a, b) => a + b;
int sum = sumDelegate(2, 3); // sum = 5
In the above example, the Func<int, int, int>
delegate type represents a method that takes two integer input parameters and returns an integer value. The lambda expression (a, b) => a + b
is used to define the method that takes two integer input parameters a
and b
, and returns their sum.
The sumDelegate
variable is assigned to the lambda expression, and can be called like a regular method with two integer input parameters to return their sum.
The Func
delegate type is often used in LINQ expressions to represent a projection, or to transform data from one type to another.
Comments
Post a Comment