Func and Action delegate

With generic delegate, there is a return can be applied to any type and any parameters (type and reasonable number) of the common commission, Func and Action. As follows (expressed in the following parameters, out indicates return results):

delegate TResult Func <out TResult> ();
delegate TResult Func <in T, out TResult> (T arg);
delegate TResult Func <in T1, in T2, out TResult> (T1 arg1, T2 arg2); //一直到 T16 delegate void Action ();
delegate void Action <in T> (T arg);
delegate void Action <in T1, in T2> (T1 arg1, T2 arg2); //一直到 T16

With such a common trust, our top Calculator generic delegate can be deleted, examples can be more concise:

public static void Calculate<T>(T[] values, Func<T,T> c) {
    for (int i = 0; i < values.Length; i++)
        values[i] = c(values[i]);
}

Func and Action delegate, except ref parameters and out parameters, can be applied to substantially any generic delegate scene, very easy to use.

Guess you like

Origin www.cnblogs.com/XiaoRuLiang/p/12422917.html