Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Browse Tutorials
C Sharp Generic delegates Func, Action and Predicate with anonymous method

C Sharp Generic delegates Func, Action and Predicate with anonymous method

08 Mar 2024
Advanced
116K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

C# Programming For Beginners Free Course

In .net 3.5 some new generic delegates -Func<T>, Action<T> and Predicate<T> were introduced. Using generic delegates, it is possible to concise delegate type means you don’t have to define the delegate statement. These delegates are the Func<T>, Action<T> and Predicate<T> delegates and defined in the System namespace.

Action<T> performs an operation on the generic arguments. Func<T> performs an operationon the argument(s) and returns a value, and Predicate<T> is used to represent a set of criteria and determine if the argument matches the criteria.

 delegate TResult Func  ();
delegate TResult Func  (T arg);
delegate TResult Func  (T1 arg1, T2 arg2);
... up to T16
 delegate void Action ();
delegate void Action  (T arg);
delegate void Action  (T1 arg1, T2 arg2);
... up to T16 

Here "in" shows the input parameters and "out" shows the return value by the delegate.

Read More - C# Interview Questions For Freshers

Generic delegate example

 using System;
class demo 
{ 
delegate void MyDelegate(string str); 
static void Main(string[] args) 
{ 
MyDelegate d = show; 
d("Hello World!"); 
Console.ReadLine();
 }
static void show(string str) 
{ 
Console.WriteLine(str); 
} 
} 

Above code can be written as using generic delegate.

 using System;
class demo 
{ 
static void Main(string[] args) 
{ 
Action<string> d = show;
d("Hello World!"); 
Console.ReadLine(); 
}
static void show(string str)
{ 
Console.WriteLine(str); 
} 
} 

Generic delegate using anonymous method

 using System;
class demo 
{ 
static void Main(string[] args) 
{ 
Action<string> d = s => Console.WriteLine(s); 
d("Hello World!"); 
}
} 
Summary

In this article I try to explain the generic delegates with example. I hope after reading this article you will be able to understand the use of generic delegates. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Take our free csharp skill challenge to evaluate your skill

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this