Delegates are one of the most commonly asked C# interview topics.
What is a Delegate in C#?
A Delegate is a type-safe function pointer.
That means:
-
A delegate holds reference to a method
-
You can pass methods as parameters
-
You can call methods dynamically
Simple Definition (Interview Ready)
A delegate is a type-safe object that holds reference to a method and allows methods to be passed as parameters.
Basic Syntax
delegate returnType DelegateName(parameters);
Example:
delegate void MyDelegate();
This delegate can hold any method that:
-
returns
void -
has no parameters
Basic Example
using System;
delegate void MyDelegate();
class Program
{
static void Main()
{
MyDelegate del = ShowMessage;
del();
}
static void ShowMessage()
{
Console.WriteLine("Hello Delegates");
}
}
Output
Hello Delegates
Delegate With Parameters
delegate int MyDelegate(int a, int b);
class Program
{
static void Main()
{
MyDelegate del = Add;
int result = del(5, 3);
Console.WriteLine(result);
}
static int Add(int a, int b)
{
return a + b;
}
}
Output:
8
Why Delegates Are Used (Very Important for Interviews)
Delegates are used for:
-
Callbacks
-
Event handling
-
Passing methods as parameters
-
Loose coupling
-
LINQ
-
Func / Action / Predicate
Real Interview Example (Callback)
delegate void Notify();
class Process
{
public void StartProcess(Notify notify)
{
Console.WriteLine("Process Started...");
notify();
}
}
class Program
{
static void Main()
{
Process p = new Process();
p.StartProcess(ShowMessage);
}
static void ShowMessage()
{
Console.WriteLine("Process Completed");
}
}
Output:
Process Started...
Process Completed
This is callback using delegate.
Multicast Delegate (Very Important Interview Question)
A delegate can call multiple methods.
delegate void MyDelegate();
class Program
{
static void Main()
{
MyDelegate del = Method1;
del += Method2;
del += Method3;
del();
}
static void Method1()
{
Console.WriteLine("Method 1");
}
static void Method2()
{
Console.WriteLine("Method 2");
}
static void Method3()
{
Console.WriteLine("Method 3");
}
}
Output:
Method 1
Method 2
Method 3
Built-in Delegates (Very Important for Interviews)
C# provides built-in delegates:
1. Action
Used when method returns void
Action<string> action = Show;
action("Hello");
static void Show(string message)
{
Console.WriteLine(message);
}
2. Func
Used when method returns value
Func<int, int, int> func = Add;
Console.WriteLine(func(5,3));
static int Add(int a, int b)
{
return a + b;
}
3. Predicate
Used when method returns bool
Predicate<int> isEven = Check;
Console.WriteLine(isEven(4));
static bool Check(int number)
{
return number % 2 == 0;
}
Delegate vs Func vs Action (Interview Question)
| Delegate | Func | Action |
|---|---|---|
| Custom delegate | Built-in delegate | Built-in delegate |
| Can return value | Returns value | Returns void |
| More verbose | Short syntax | Short syntax |
Delegates with Lambda Expression (Modern C#)
Func<int, int, int> add = (a, b) => a + b;
Console.WriteLine(add(5, 3));
Output:
8
Delegates in Real Life Example (Sorting)
List<int> numbers = new List<int> {5,2,8,1};
numbers.Sort((a, b) => a.CompareTo(b));
numbers.ForEach(n => Console.WriteLine(n));
Delegates vs Events (Very Important Interview Question)
| Delegate | Event |
|---|---|
| Method reference | Wrapper around delegate |
| Can be invoked anywhere | Can only be invoked inside class |
| Used for callbacks | Used for event handling |
Most Asked Interview Questions
1. What is Delegate?
Type-safe function pointer.
2. What is Multicast Delegate?
Delegate calling multiple methods.
3. What is Action?
Delegate that returns void.
4. What is Func?
Delegate that returns value.
5. What is Predicate?
Delegate that returns bool.
6. Delegate vs Event?
Event is wrapper around delegate.
Short Interview Definition (Best Answer)
A delegate in C# is a type-safe function pointer that holds reference to one or more methods and allows methods to be passed as parameters.
