Using Delegates in C#
What are Delegates?
Delegates are a type that represents references to methods with a specific parameter list and return type. When you instantiate a delegate, you can associate its instance with any method that matches its signature. You can invoke the method through the delegate instance.
Defining a Delegate
To define a delegate, use the delegate keyword followed by a method signature. Here’s an example:
public delegate void PrintMessage(string message);
This defines a delegate named PrintMessage that can reference any method that takes a string parameter and returns void.
Instantiating and Using a Delegate
Once a delegate is defined, you can instantiate it and use it to call methods. Here's how you can do it:
class Program
{
// Define the delegate
public delegate void PrintMessage(string message);
// A method that matches the delegate signature
public static void Print(string msg)
{
Console.WriteLine(msg);
}
static void Main(string[] args)
{
// Instantiate the delegate
PrintMessage pm = new PrintMessage(Print);
// Use the delegate to call the method
pm("Hello, Delegates!");
}
}
In this example, PrintMessage pm = new PrintMessage(Print); creates a delegate instance pm and associates it with the Print method. The method is then invoked using pm("Hello, Delegates!");.
Multicast Delegates
Delegates in C# can also be multicast, meaning they can hold references to more than one method. When you invoke a multicast delegate, all the methods it’s referencing are invoked in order. Here’s an example:
class Program
{
public delegate void PrintMessage(string message);
public static void Print(string msg)
{
Console.WriteLine("Print: " + msg);
}
public static void PrintUpperCase(string msg)
{
Console.WriteLine("UpperCase: " + msg.ToUpper());
}
static void Main(string[] args)
{
PrintMessage pm = Print;
pm += PrintUpperCase;
pm("Hello, Multicast Delegates!");
}
}
In this example, the delegate pm is associated with both Print and PrintUpperCase methods. When pm("Hello, Multicast Delegates!"); is called, both methods are invoked.
Using Delegates with Anonymous Methods
Anonymous methods provide a way to write inline code for a delegate. This can be useful for short pieces of code that will only be used once. Here’s an example:
class Program
{
public delegate void PrintMessage(string message);
static void Main(string[] args)
{
PrintMessage pm = delegate(string msg)
{
Console.WriteLine("Anonymous: " + msg);
};
pm("Hello, Anonymous Methods!");
}
}
In this example, an anonymous method is assigned to the delegate pm. This allows the delegate to be used without defining a separate method.
Using Delegates with Lambda Expressions
Lambda expressions provide a more concise way to write inline code for delegates. Here’s an example using a lambda expression:
class Program
{
public delegate void PrintMessage(string message);
static void Main(string[] args)
{
PrintMessage pm = (msg) => Console.WriteLine("Lambda: " + msg);
pm("Hello, Lambda Expressions!");
}
}
In this example, a lambda expression (msg) => Console.WriteLine("Lambda: " + msg) is assigned to the delegate pm.
Benefits of Using Delegates
Delegates provide several benefits:
- Encapsulation of method references
- Flexibility in method invocation
- Support for callback methods
- Facilitate event-driven programming
Conclusion
Delegates are a powerful feature in C# that allow methods to be passed as parameters, stored in variables, and used for event handling. By understanding and using delegates, you can write more flexible and reusable code.