C# | Func delegate

Prerequisite: Delegates in C#

When we create a custom delegate we have to follow the following steps:

Step 1: Declare a custom delegate with the format which is exactly equal to the method.

Step 2: Create the object of custom delegate.



Step 3: Invoke the method.

By using these steps, we create a custom delegate as shown in the below program. But the problem is that for creating a delegate we need to follow the above procedure. To overcome this situation C# provides a built-in delegate that is Func. Using Func delegate you need not follow the following procedure to create a delegate.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate how to 
// create custom delegates
using System;
  
class GFG {
  
    // Declaring the delegate
    public delegate int my_delegate(int s, int d,
                                   int f, int g);
  
    // Method
    public static int mymethod(int s, int d,
                              int f, int g)
    {
        return s * d * f * g;
    }
  
    // Main method
    static public void Main()
    {
  
        // Creating object of my_delegate
        my_delegate obj = mymethod;
        Console.WriteLine(obj(12, 34, 35, 34));
    }
}

chevron_right


Output:

485520

A Func is a built-in generic type delegate. This delegate saves you from defining a custom delegate like as shown in the above example and make your program more readable and optimized. As we know that, Func is a generic delegate so it is defined under System namespace. It can contain minimum 0 and maximum of 16 input parameters in it and contain only one out parameter. The last parameter of the Func delegate is the out parameter which is considered as return type and used for the result. Func is generally used for those methods which are going to return a value, or in other words, Func delegate is used for value returning methods. It can also contain parameters of the same type or of different types.

Syntax:

// Zero normal parameters and one result parameter
public delegate TResult Func<out PResult>();

// One normal parameter and one result parameter
public delegate TResult Func<in P, out PResult>(P arg);

// Two normal parameters and one result parameter
public delegate TResult Func<in P1, in P2, out PResult>(P1 arg1, P2 arg2);

// Sixteen normal parameters and one result parameter
public delegate TResult Func<in P1, in P2, in P3, in P4, in P05, in P6, in P7, in P8, in P9, in P10, in P11, in P12, in P13, in P14, in P15, in P16, out PResult>(P1 arg1, P2 arg2, P3 arg3, P4 arg4, P5 arg5, P6 arg6, P7 arg7, P8 arg8, P9 arg9, P10 arg10, P11 arg11, P12 arg12, P13 arg13, P14 arg14, P15 arg15, P16 arg16);



Here, P1, P2….P16 are the type of input parameters, PResult is the type of output parameter, and arg1….arg16 are the parameter of the method that the Func delegate encapsulates.

Example 1: Here, we use a Func delegate to create a delegate only in a single line without using the above procedure. This Func delegate contains four input parameters and one output parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate Func delegate
using System;
  
class GFG {
  
    // Method
    public static int mymethod(int s, int d, int f, int g)
    {
        return s * d * f * g;
    }
  
    // Main method
    static public void Main()
    {
  
        // Using Func delegate
        // Here, Func delegate contains
        // the four parameters of int type
        // one result parameter of int type
        Func<int, int, int, int, int> val = mymethod;
        Console.WriteLine(val(10, 100, 1000, 1));
    }
}

chevron_right


Output:

1000000

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate Func delegate
using System;
  
class GFG {
  
    // Method
    public static int method(int num)
    {
        return num + num;
    }
  
    // Main method
    static public void Main()
    {
  
        // Using Func delegate
        // Here, Func delegate contains 
        // the one parameters of int type
        // one result parameter of int type
        Func<int, int> myfun = method;
        Console.WriteLine(myfun(10));
    }
}

chevron_right


Output:

20

Important Points:

  • The last parameter in Func Delegate is always an out parameter which is considered as a return type. It is generally used for the result.
  • You can also use a Func delegate with an anonymous method. As shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Func<int, int, int> val = delegate(int x, int y, int z)
    {
        return x + y + z;
    };

    chevron_right

    
    

  • You can also use a Func delegate with the lambda expressions. As shown in the below example:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    Func<int, int, int, int> val = (int x, int y, int z) = > x + y + z;

    chevron_right

    
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : HIMANSHUGARG8



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.