Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Function Signature 
3.
Example 
4.
Func Delegate With Anonymous Methord
5.
Func Delegate With Lambda Expressions
6.
Frequently Asked Questions
6.1.
The last parameter in Func delegate represents?
6.2.
In which namespace Func delegate is specified?
6.3.
How many parameters can a Func delegate accept?
6.4.
Can Func delegate be used with an anonymous function?
6.5.
Can Func delegate be used with lambda expression?
7.
Conclusion
Last Updated: Mar 27, 2024

C# Func Delegate

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction 

A Func delegate is a generic type of delegate that is built-in. This delegate eliminates the need to define a custom delegate, as demonstrated in the preceding example, and makes your application more understandable and streamlined. Because Func is a generic delegate, it is specified in the System namespace. It can have a minimum of 0 and a maximum of 16 input parameters, but only one out parameter. Func is typically used for methods that will return a value, whereas Func delegate is used for methods that will return a value. It may also include parameters of the same or other sorts.

Function Signature 

The System namespace defines the Func delegate, which accepts one input argument and one output parameter, as seen below:

namespace System { 
	public delegate TResult Func<in T, out TResult>(T arg); 
}


The last argument in the angle brackets <> is regarded as the return type, while the rest are input parameter types.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example 

class Example {
    public static int method(int num)
    {
        return num + num;
    }
      static public void Main()
    {
        Func<int, int> myfun = method;
        Console.WriteLine(myfun(10));
    }
}


Output:

20

Func Delegate With Anonymous Methord

An anonymous method can also be used with a Func delegate. As seen in the case below.


Example:

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

Func Delegate With Lambda Expressions

With lambda expressions, you may also utilize a Func delegate. As seen in the case below.


Example:

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

Frequently Asked Questions

The last parameter in Func delegate represents?

The last parameter in the Func delegate represents the return type. You need to specify the return type at the end of the parameters.

In which namespace Func delegate is specified?

Function delegate is specified in the System namespace. System.Runtime.dll. Encapsulates a single-parameter method that returns a result of the type defined by the TResult parameter.

How many parameters can a Func delegate accept?

It can have as less as 0 input parameters and as many as 16 input parameters, but only one output parameter. The Func delegate's final parameter is the out argument, which is a return type and is used for the outcome.

Can Func delegate be used with an anonymous function?

Yes, you can use Fun delegate with an anonymous function. The function delegate does not support ref and out arguments. An anonymous method or lambda expression can be utilized with the Func delegate type.

Can Func delegate be used with lambda expression?

Yes, Func delegate can be used with a lambda expression.  An anonymous method or lambda expression can be utilized with the Func delegate type.

Conclusion

In this article, we had a look at Func Delegate in C# and learned its implementation. We hope that this blog has helped you enhance your knowledge regarding Func Delegate and if you would like to learn more, check out Delegates in C#  and Methods in C# also other articles on Coding Ninjas Studio

Recommended Readings: 

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass