Curriculum
Delegates in C# are one of the most powerful features of the .NET Framework and form the foundation for events, lambda expressions, asynchronous programming, LINQ, callbacks, and modern software architecture. Delegates in C# allow methods to be treated as objects, enabling developers to pass methods as parameters, store references to methods, and execute methods dynamically at runtime.
Every professional .NET developer uses Delegates in C# while working with ASP.NET Core Applications, MVC Projects, Web APIs, Windows Applications, Cloud Applications, Microservices, Game Development, and Enterprise Software Systems.
Understanding Delegates in C# is essential because they are heavily used throughout the .NET ecosystem and are frequently asked about in technical interviews.
A Delegate in C# is a type-safe function pointer.
A delegate stores a reference to a method and allows that method to be called later.
In simple terms:
Variable Stores Data
Delegate Stores Methods
Example:
public delegate void MessageDelegate();
The delegate can now point to any method that matches its signature.
Delegates in C# help developers:
Delegates are one of the core building blocks of modern .NET development.
Consider a TV Remote.
The remote does not know how the television works internally.
It only knows which button triggers which action.
Example:
Power Button → Power Method
Volume Button → Volume Method
Channel Button → Channel Method
Delegates work similarly by referencing methods without needing to know implementation details.
Basic syntax:
public delegate void
MyDelegate();
Structure:
delegate
Return Type
Delegate Name
Parameters
The delegate signature must match the method signature.
Step 1: Create Delegate
public delegate void
DisplayDelegate();
Step 2: Create Method
public static void Display()
{
Console.WriteLine(
"Welcome to C#");
}
Step 3: Assign Delegate
DisplayDelegate del =
Display;
Step 4: Invoke Delegate
del();
Output:
Welcome to C#
The delegate successfully calls the method.
Example:
public delegate void
StudentDelegate(
string name);
Method:
public static void DisplayStudent(
string name)
{
Console.WriteLine(
name);
}
Usage:
StudentDelegate del =
DisplayStudent;
del("Rahul");
Output:
Rahul
The method signature matches the delegate signature.
Example:
public delegate int
CalculationDelegate(
int a,
int b);
Method:
public static int Add(
int a,
int b)
{
return a + b;
}
Usage:
CalculationDelegate del =
Add;
int result =
del(10,20);
Output:
30
Delegates can work with return values.
A Multicast Delegate can reference multiple methods.
Example:
public delegate void
MessageDelegate();
Methods:
public static void Welcome()
{
Console.WriteLine(
"Welcome");
}
public static void Thanks()
{
Console.WriteLine(
"Thank You");
}
Assignment:
MessageDelegate del =
Welcome;
del += Thanks;
Invocation:
del();
Output:
Welcome
Thank You
Both methods execute.
Example:
del -= Thanks;
Now:
del();
Output:
Welcome
The second method has been removed.
A multicast delegate maintains an invocation list.
Example:
Welcome()
Thanks()
Goodbye()
When invoked:
del();
All methods execute sequentially.
.NET provides built-in generic delegates.
Examples:
Action
Func
Predicate
These eliminate the need to create custom delegates in many situations.
Action represents a method that:
Returns Void
Example:
Action<string> display =
name =>
Console.WriteLine(
name);
Usage:
display("Rahul");
Output:
Rahul
Action is widely used in modern C# applications.
Example:
Action<string,int>
student =
(name,age) =>
{
Console.WriteLine(
name);
Console.WriteLine(
age);
};
Usage:
student(
"Rahul",
21);
Output:
Rahul
21
Func represents a method that:
Returns a Value
Example:
Func<int,int,int>
add =
(a,b) =>
a + b;
Usage:
int result =
add(10,20);
Output:
30
Func is heavily used with LINQ.
Example:
Func<int,int,int>
Meaning:
Parameter 1 → int
Parameter 2 → int
Return Type → int
The last type always represents the return value.
Predicate represents a method returning:
Boolean
Example:
Predicate<int>
isEven =
number =>
number % 2 == 0;
Usage:
bool result =
isEven(10);
Output:
True
Predicate is frequently used for filtering.
Example:
public static void Process(
Action action)
{
action();
}
Usage:
Process(
() =>
Console.WriteLine(
"Processing"));
Output:
Processing
This technique is widely used in frameworks.
Method:
public static void Download(
Action callback)
{
Console.WriteLine(
"Downloading...");
callback();
}
Usage:
Download(
() =>
Console.WriteLine(
"Download Complete"));
Output:
Downloading...
Download Complete
Delegates enable callback mechanisms.
Transaction Processing
Notifications
Audit Logging
Order Processing
Payment Callbacks
Inventory Updates
Appointment Notifications
Medical Alerts
Patient Updates
Attendance Alerts
Exam Notifications
Student Reports
Delegates support event-driven architectures.
Events are built on delegates.
Example:
public event EventHandler
ButtonClicked;
Internally:
Event
→ Delegate
→ Method
Understanding delegates is essential before learning events.
Methods can be passed dynamically.
Code becomes reusable.
Classes become independent.
Delegates enable events.
Tasks and callbacks rely on delegates.
These advantages make delegates indispensable.
Delegate and method signatures must match.
Bad:
del();
if:
del == null
Always verify before invocation.
Use:
Action
Func
Predicate
when possible.
Generic delegates simplify development.
A Delegate is a type-safe function pointer that stores references to methods.
A Multicast Delegate can reference multiple methods.
Action represents methods that return void.
Func represents methods that return values.
Predicate represents methods that return Boolean values.
Delegates enable callbacks, events, asynchronous programming, and flexible software architecture.
Delegates are type-safe function pointers that reference methods.
Delegates enable callbacks, events, and dynamic method execution.
A Multicast Delegate can execute multiple methods through a single delegate instance.
Action returns void, while Func returns a value.
Predicate is a delegate that returns a Boolean value.
Delegates are fundamental to events, LINQ, asynchronous programming, and enterprise application development.
WhatsApp us