Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up
C Sharp Generic delegates Func, Action and Predicate with anonymous method

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

19 Sep 2025
Advanced
124K Views
8 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

Generic delegates in C#: An Overview

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 which means you don’t have to define the delegate statement. These delegates are the Func<T>, Action<T>, and Predicate<T> delegates and are defined in the System namespace.

In this C# Tutorial, we will explore more about generic delegates which will include generic delegates with examples, func action predicates with examples, and Generic delegates using an anonymous method. Certified C# professionals earn 20% more than non-certified peers. Get your Free C Sharp Course with Certification today!

What are the generic delegates?

  • Delegates in C# are nothing but pointers to function.
  • They are used for implementing events and call-back methods.
  • Func, Action, and Predicate are the delegates defined in C# 3.0
  • Func, Action, and Predicate are generic inbuilt delegates.
  • Let's see the types of delegates with its example in C# Compiler.

1.Func Delegate in C#:

  • Func is a generic delegate present in the System namespace.
  • Func takes one or more input parameters and returns one out parameter and the last parameter is considered as a return value.
  • It can include 0 to 16 input parameters of different types but it must have one return type.

Example:

Func delegate with two input parameters

 Func func1 = DelegateClass.Add;  
int value = func1(22, 44);  
TParameter = 22,44;  
TOutput = value = 66;    

Func delegate with Anonymous Methods

 Func with Anonymous methods:  
Func func= delegate(intx,int y){ return (x+y); };  
int result = func4(2,3);    

2. Action Delegate in C#

  • Action present in the System namespace.
  • It takes one or more than one input parameter and returns nothing.
  • It does not return any value.

Example:

Action delegate with two input parameters

 Action action1=DelegateClass.ShowEmploye;  
action1(44,"Jayanti");  
TParameter = 44,Jayanti;  
TOutput = Not available (No return value)   

Action delegate with Anonymous Methods

 Action action = delegate(String msg)  
{  
    Console.WriteLine(msg);  
};  
action("Jayanti");     

3. Predicate Delegate in C#

  • A predicate delegate is also an inbuilt generic delegate and is present in the System namespace.
  • predicate is used to verify certain criteria of the method and returns output as Boolean, either True or False.
  • The predicate can be used with the method, anonymous, and lambda expressions.

Example:

Predicate delegate with two input parameters

 Predicate predicate = DelegateClass.IsNumeric;
bool number = predicate("987654");

Predicate delegate with Anonymous Methods

 Predicate predicate = delegate(string str)  
{  
    double retNum;  
    bool isNum = Double.TryParse(Convert.ToString(str), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);  
    return isNum;  
};  
bool found = predicate("987654");   

Generic delegates in a nutshell

Action<T> operates on the generic arguments. Func<T> performs an operation on 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); 
} 
}

The above code can be written using a 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 an 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 examples. I hope after reading this article you will be able to understand the use of generic delegates.

Over 10,000 companies seek certified .NET full stack developers in India. Start your journey with our full stack .NET developer course and seize top opportunities!

FAQs

The Func Generic Delegate in C# is present in the System namespace.

Programmers use Action delegates when a method does not return a value, and Func delegates when a method returns a value.

The anonymous method is useful when the user wants to create an inline method and also wants to pass parameters in the anonymous method like other methods.

Take our Csharp skill challenge to evaluate yourself!

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

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
02 Nov
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
02 Nov
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Angular Certification Training
02 Nov
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure Developer Certification Training
04 Nov
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
09 Nov
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification