Please enable Javascript to correctly display the contents on Dot Net Tricks!

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

  Author : Shailendra Chauhan
Posted On : 20 Jun 2012
Total Views : 97,305   
Updated On : 22 Feb 2013
 

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.

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.



Free Interview Books
 
COMMENTS (0)
5 AUG
ASP.NET MVC with Angular4 Development (Online)

10:30 Am to 12:30 PM IST (+ 5:30 GMT)

Know More
3 AUG
ASP.NET Core with Angular4 Development (Online)

07:00 AM - 9:00 AM IST(+5:30 GMT)

Know More
29 JUL
Angular2 and Angular4 Development (Online)

08:00 AM - 10:00 AM IST(+5.30 GMT)

Know More
29 JUL
ASP.NET MVC with Angular4 Development (Classroom)

09:30 AM - 11:00 AM IST (+5:30GMT)

Know More
29 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

09:00 AM - 11:00 AM IST(+5:30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

Know More
26 JUL
ASP.NET MVC with Angular4 Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

22 JUL
MEAN Stack 2 Development (Classroom)

04:00 PM - 05:30 PM IST(+5:30GMT)

20 JUL
Hadoop Development cum Administration (Online)

07:00 AM - 09:00 AM IST(+5:30 GMT)

17 JUL
NodeJS with Angular4 Development (Online)

08:00 PM - 09:30 PM IST(+5.30 GMT)

15 JUL
ASP.NET MVC with Angular4 Development (Online)

03:30 PM - 05:30 PM IST(+5.30 GMT)

1 JUL
ASP.NET MVC with Angular4 Development (Classroom)

08:00 AM - 09:30 AM IST

1 JUL
MEAN Stack 2 Development (Classroom)

11:00 AM - 12:30 PM IST(+5:30 GMT)

30 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

09:30 PM - 11:00PM IST(+5:30GMT)

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

20 MAY
NodeJS with Angular4 Development (Classroom)

05:00 PM - 07:00 PM

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST(+5.30 GMT)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+