Pro EP 70 : Difference b/w Delegate, Func, Action and Predicate ?
Delegates, Func, Action and Predicate in C# ?
Delegates is a pointer to function. It helps us in communication and call backs.
We have three built in generic delegates:
1) Func: It takes a generic input and returns generic output
2) Action: It takes a generic input and returns nothing
3) Predicate: It takes a generic input and always returns Boolean
These generic delegates are used extensively in built in classes of .NET and we can use them to make our delegates shorter
Do you wanna see them in real example:
- Create a class Student with some properties
- Make a list of Student type
- Add some values
Following LINQ methods uses delegates:
.Select method takes Func
.ForEach method takes an Action
.FindAll method takes a Predicate
How often do you use them in code ?
Whenever you’re ready, there are 4 ways I can help you
- Subscribe to my Weekly .NET Newsletter of C#/.NET with b6000+ Software Engineers.
- Promote yourself to 6000+ subscribers by Sponsoring my Newsletter
- Download my eBook at Gum Road which contains 30+ .NET Tips (With 2800+ Downloads)
- To get 100+ free and paid resources for learning C# and .NET visit this GitHub Repository