READ AND KNOW FUNC AND ACTION DELEGATES IN .NET
The .NET support edition 3.5 introduced digit newborn sets of generic, parameterised delegates titled Func and Action. The Func assign crapper be utilised to digest a method that accepts between set and quaternary arguments and returns a value. The Action assign also represents methods with set to quaternary parameters but differs from Func in that the method staleness convey void.
The newborn delegates crapper be utilised to turn the sort of delegates that you delimitate explicitly. In situations where you would requirement a assign that matches digit of the predefined Func or Action signatures, you haw end to ingest the built-in version. You should study the denotive of the assign however, as “Func” or “Action” haw not impart your aim as understandably as added name.
One of the key reasons for the launching of Func and Action is their relation with lambda expressions. Every lambda expression’s inexplicit identify is digit of these generic delegates. This effectuation that lambda expressions crapper be passed to method parameters of the pertinent identify without explicitly creating a delegate. Many of the LINQ accepted ask operators accept Func arguments to verify plus of this feature.
Func Delegate
There are fivesome variations upon the Func delegate, apiece allowing a assorted sort of parameters to be represented. In apiece case, the convey continuance and the parameters are circumscribed as generic types, allowing the types to depart according to the method that the assign encapsulates. The fivesome signatures are shown below. Note that the convey continuance of the method is ever the terminal surroundings in the itemize of types.
public assign TResult Func<TResult>() public assign TResult Func<T, TResult>(T arg) public assign TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2) public assign TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3) public assign TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
Using Func<TResult>
The most base var. of Func is Func<TResult>. This assign represents methods that convey a continuance but hit no parameters. We crapper shew this with the mass distribution code. In the example, a newborn assign is created using an nameless method. The method returns a double-precision floating-point sort containing a set evaluate value.
Func<double> taxRate = assign { convey 17.5; };
Console.WriteLine("{0}%", taxRate()); // Outputs "17.5%"
We crapper create the aforementioned functionality using a lambda expression. Any lambda that accepts no arguments and returns a threefold is of the identify Func<double>. To demonstrate, essay executing the following:
Func<double> taxRate = () => 17.5;
Console.WriteLine("{0}%", taxRate()); // Outputs "17.5%"
Using Func with Parameters
Func crapper equal methods with up to quaternary parameters. In the incoming warning a Func assign is utilised to meaning an nameless method that accepts threesome number arguments and returns a daylong integer.
Func<int, int, int, long> multiply = delegate(int a, int b, int c) { convey a * b * c; };
Console.WriteLine(multiply(2, 3, 4)); // Outputs "24"
Again, the cipher crapper be recreated using a lambda countenance in locate of the nameless method. In this case, the threesome parameters are titled a, b and c. The constant types do not requirement to be included in the lambda countenance as they, and the convey type, are inferred by the compiler.
Func<int, int, int, long> multiply = (a, b, c) => a * b * c; Console.WriteLine(multiply(2, 3, 4)); // Outputs "24"