System.Func<> encapsulates a method that has zero '0' to nine '9' parameter(for now in .NET 4 previously in .NET 3.5 it had 0-4 parameters and one return type) and returns a value of the type specified by the TResult parameter. It is useful because it does not require you to write custom delegate types. This makes it much easier to interop delegates with the same signature.
In all versions of System.Func, the last generic argument is the return type, all the others are the types of the parameters, in order.
Syntax for no Parameter
Func<Tresult>
Sample :-
//return type is boolean and no parameter for the method.
Func<bool> methodCall = output.SendToFile;
Assiging Lambda Expression to the Func delegate.
Func<bool> methodCall = () => output.SendToFile();
Syntax for more paramters
Suppose you have a function such as:
private static string toUpper(string str)
{
return str.ToUpport();
}
There is a version of System.Func that takes two generic arguments, the first being the type of the first parameter, the second being the return type. As such, you could write:
Func<string,string> myFunction = toUpper;
string getFunc = myFunction("SIvakuMar");
// s is now "SIVAKUMAR"
Maximum Paramter for Func Delegate
Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> Delegate Encapsulates a method that has nine parameters and returns a value of the type specified by the TResult parameter
Action Delegate
Action encapsulates a method that has nine parameters(.NET 4.0, previosuly in 3.5 it takes 4 parameters) and does not return a value.
To reference a method that has nine parameters and returns void (or in Visual Basic, that is declared as a Sub rather than as a Function), use the generic Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> delegate.
When introduced in .NET 2.0, Action method Represents the method that performs an action on the specified object. and in 3.5 Action method that takes a single parameter and does not return a value. (only the definition get changed and not the syntax) and in .NET 4.0
No comments:
Post a Comment