Built-in delegates

So far, we have seen how we can create custom delegates and use them in our program. C# comes with a couple of built-in delegates, which developers can use instead of having to create custom delegates. They are as follows:

  • Func
  • Action

Func takes zero or more parameters and returns one value as an out parameter, whereas Action accepts zero or more parameters but returns nothing.

There is no requirement to declare an explicit delegate when working with Func or Action:

public delegate TResult Func<out TResult>();

Action can be defined as follows:

public delegate void Action();

As we mentioned earlier, both take zero or more parameters. C# supports 16 different forms of both delegates, all of which can be used in our program.

The general form of Func with two or more parameters is as follows. It takes comma-separated in and out parameters, where the last parameter is always an out parameter called TResult:

public delegate TResult Func<in T1,in T2,in T3,in T4,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);

Similar to Func, here is the general form for Action with two or more parameters:

public delegate void Action<in T1,in T2,in T3,in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123