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 with void 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

  1. Action β†’ method with no return value

    Action<string> action = ShowMessage;
    action("Hi!");
  2. Func β†’ method that returns a value

    Func<int, int, int> add = (x, y) => x + y;
    Console.WriteLine(add(3,4)); // 7
  3. 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.