- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between Func delegate and Action delegate in C#?
A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance.
Func Delegate
Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.
Below is the Func delegate with two input and an output parameter.
Func<string, string, string > Append;
Example
using System; namespace DemoApplication { class Program { static void Main(string[] args) { Func<string, string, string> func = Append; string fullName = func("Michael", "Jackson"); Console.WriteLine(fullName); Console.ReadLine(); } static string Append(string firstName, string lastName) { return firstName + lastName; } } }
Output
MichaelJackson
Action Delegate
Action is a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.
Below is the Func delegate with two input and an output parameter.
Func<string, string,> Print;
Example
using System; namespace DemoApplication { class Program { static void Main(string[] args) { Action func = AppendPrint; func("Michael", "Jackson"); Console.ReadLine(); } static void AppendPrint(string firstName, string lastName) { string fullName = firstName + lastName; Console.WriteLine($"{fullName}"); } } }
Output
The output of the above code is
MichaelJackson