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 to follow the following procedure to create a delegate.
Example:
// 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)); } } |
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 output parameter. The last parameter of the Func delegate is the output parameter or result parameter. 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. It does not contain ref or out parameters.
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 parameter and one output parameter.
// 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)); } } |
Output:
1000000
Example 2:
// 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)); } } |
Output:
20
Important Points:
- You can also use a Func delegate with an anonymous method. As shown in the below example:
Example:
Func<int,int,int> val =delegate(intx,inty,intz){returnx + y + z;};chevron_rightfilter_none - You can also use a Func delegate with the lambda expressions. As shown in the below example:
Example:
Func<int,int,int,int> val = (intx,inty,intz) = > x + y + z;chevron_rightfilter_none
Recommended Posts:
- C# | Action Delegate
- C# | Predicate Delegate
- How to set the Auto Size Mode of FlowLayoutPanel in C#?
- How to set Font in FlowLayoutPanel in C#?
- How to set the Location of the FlowLayoutPanel in C#?
- How to set a Check Box in the DateTimePicker in C#?
- How to style the Border of the FlowLayoutPanel in C#?
- How to set the Name of FlowLayoutPanel in C#?
- How to Style the Border of the RichTextBox in C#?
- How to set the Visibility of the FlowLayoutPanel in C#?
- How to set the Flow Direction of FlowLayoutPanel in C#?
- How to set the Size of the RichTextBox in C#?
- How to set Up and Down Button in DateTimePicker in C#?
- How to set Foreground Color of FlowLayoutPanel in C#?
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



