Delegates and Multicast Delegate in C#

 

Delegate and Multicast Delegate

 A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.

Key Points about Delegates:

  1. Type-Safe Method References: A delegate is type-safe, meaning it can only reference methods that match its signature (i.e., return type and parameters).

  2. Encapsulating Methods: A delegate can reference a single method or multiple methods (multicast delegates). When a delegate is invoked, it calls the method(s) it references.

  3. Delegates and Events: Delegates are often used in C# for events and callback mechanisms. Events in C# are built upon delegates.

All delegates are implicitly derived from the System.Delegate class

Creating Delegates, You can declare a delegate type and then instantiate it to reference methods that match its signature.


// Declare a delegate type

public delegate void MyDelegate(string message);

Create Methods that Match the Delegate's Signature:


public class ExampleClass

{

    public static void DisplayMessage(string message)

    {

        Console.WriteLine("Message: " + message);

    }

}



Instantiating Delegates and assign method 


class Program

{

    static void Main(string[] args)

    {

        // Create an instance of the delegate, pointing to the DisplayMessage method

        MyDelegate del = new MyDelegate(ExampleClass.DisplayMessage);

        

        // Invoke the method via the delegate

        del("Hello, Delegate!");

    }

}


Multicast Delegate

Delegates in C# can point to more than one method, which is called a multicast delegate. When invoked, all the methods in the delegate’s invocation list are called.


public delegate void Notify(string message);


public class Program

{

    public static void NotifyByEmail(string message)

    {

        Console.WriteLine("Sending Email: " + message);

    }


    public static void NotifyBySMS(string message)

    {

        Console.WriteLine("Sending SMS: " + message);

    }


    static void Main(string[] args)

    {

        Notify notifyDelegate = NotifyByEmail;

        notifyDelegate += NotifyBySMS;  // Add another method to the invocation list


        // Call all methods in the delegate's list

        notifyDelegate("Important Update");

    }

}




Anonymous Methods and Lambda Expression with Delegates

Instead of creating a separate method, you can use lambda expressions to directly assign a method body to a delegate:

Notify notify = (message) => Console.WriteLine("Lambda Notification: " + message);

notify("Lambda test");

Use Cases for Delegates:

  • Event handling: Delegates are a core part of event handling in C#.

  • Callback methods: Passing methods as arguments to other methods.

  • LINQ: Delegates (specifically Func and Action) are often used in LINQ queries.

Asynchronous Programming: Delegates are used in asynchronous programming for callbacks.

Comments

Post a Comment

Popular posts from this blog

Performance Optimization in Sitecore

Strategies for Migrating to Sitecore from legacy or upgrading from older Sitecore

Azure Event Grid Sample code