Letβs go over delegates in C# β a fundamental concept for type-safe function references.
π 1. What is a Delegate?
-
A delegate is a type that represents references to methods with a specific signature.
-
In other words, itβs like a pointer to a function that is type-safe.
-
Delegates allow methods to be passed as parameters or assigned to variables.
π 2. Why Use Delegates?
-
Callback methods β pass a method to another method to be called later
-
Event handling β events in C# use delegates internally
-
Decoupling β allows objects to interact without knowing each other directly
π 3. Basic Syntax
// Step 1: Declare a delegate
public delegate void MyDelegate(string message);
// Step 2: Define methods matching the delegate signature
public class Program
{
public static void ShowMessage(string msg)
{
Console.WriteLine(msg);
}
static void Main()
{
// Step 3: Instantiate delegate and assign method
MyDelegate del = ShowMessage;
// Step 4: Call method via delegate
del("Hello from delegate!");
}
}
Output:
Hello from delegate!
MyDelegate
can reference any method withvoid
return type and a single string parameter`.
π 4. Multicast Delegates
-
A delegate can reference multiple methods.
-
Use
+
or+=
to combine,-=
to remove.
public static void AnotherMessage(string msg)
{
Console.WriteLine("Another: " + msg);
}
MyDelegate del = ShowMessage;
del += AnotherMessage;
del("Hello again!");
Output:
Hello again!
Another: Hello again!
- Both methods are called in order.
π 5. Built-in Delegates
-
Action β method with no return value
Action<string> action = ShowMessage; action("Hi!");
-
Func β method that returns a value
Func<int, int, int> add = (x, y) => x + y; Console.WriteLine(add(3,4)); // 7
-
Predicate β method returning a
bool
Predicate<int> isEven = x => x % 2 == 0; Console.WriteLine(isEven(4)); // True
π 6. Difference Between Delegate and Event
-
Delegate β can be invoked anywhere you have access to it
-
Event β can only be invoked inside the class that declared it
π 7. Analogy
- Delegate β like a remote control: you assign a method (device) to the delegate (remote), then pressing the remote calls the device.
β Tip:
-
Delegates are at the heart of events, callbacks, and functional programming patterns in C#.
-
Modern C# often uses Action, Func, lambdas, but understanding delegates is crucial for events.