C# - Single-Cast Delegate
In C#, delegates are broadly categorized into two types based on the number of methods they can reference: single-cast and multicast. A single-cast delegate is a delegate that points to a single method at a time. In other words, it references only one method. If you assign a new method to a single-cast delegate, it will override the previous method reference.
Example:
Here's a simple illustration of a single-cast delegate:
using System;
// Declare a delegate - It can point to any method that has a void return type and takes a single string as a parameter.
public delegate void GreetingDelegate(string message);
public class Program
{
public static void Main()
{
// Create a delegate instance and assign the SayHello method to it
GreetingDelegate greeting = SayHello;
greeting("Alice"); // Outputs: Hello, Alice!
// Reassign the delegate instance to the SayGoodbye method
greeting = SayGoodbye;
greeting("Bob"); // Outputs: Goodbye, Bob!
}
public static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}!");
}
public static void SayGoodbye(string name)
{
Console.WriteLine($"Goodbye, {name}!");
}
}
This C# program defines a delegate called GreetingDelegate
that can point to any method with a void return type and a single string parameter.
The output of the program would be:
Hello, Alice!
Goodbye, Bob!
In the above example:
- A delegate named
GreetingDelegate
is defined, which can point to any method that takes a single string parameter and returns void.
- Initially, an instance of this delegate (
greeting
) is created and assigned the SayHello
method.
- Later, the same delegate instance is reassigned to the
SayGoodbye
method. This demonstrates the single-cast nature of the delegate: at any given time, it can point to only one method.
- When the delegate instance is invoked, it calls the method it currently points to.
Although delegates in C# inherently support multicasting, the above example restricts itself to single casting by reassigning the delegate each time, ensuring it points to only one method at a time.
- Definition: A single-cast delegate is a delegate that points to a single method at a time. It cannot point to multiple methods simultaneously.
- Overwriting: Assigning a new method to a single-cast delegate will overwrite the previous method reference. It doesn't add to the invocation list; instead, it replaces the existing method.
- Type Safety: Like all delegates in C#, single-cast delegates are type-safe. This means they can only reference methods that match their declared signature.
- Delegate Instantiation: You can create an instance of a single-cast delegate using the new keyword followed by the delegate name, or simply assign a method directly to the delegate instance.
- Invocation: Invoking a single-cast delegate calls the method it points to. If the delegate instance is null (not assigned to any method), invoking it will throw a
NullReferenceException
.
- Reassignment: You can easily reassign a single-cast delegate to point to another method with the appropriate signature.
- Usage: Single-cast delegates are often used in scenarios where a clear one-to-one relationship between the delegate and a method is desired, ensuring no unintentional invocation of multiple methods.
- Inheritance: Delegates, including single-cast ones, are derived from the
System.Delegate
class. This provides various methods like DynamicInvoke
and properties like Method
and Target
.
- Method Group Conversion: When assigning a method to a delegate, C# performs an implicit conversion known as a method group conversion, making the syntax concise.
- Limitation: Remember that the inherent nature of delegates in C# supports multicasting (pointing to multiple methods). The concept of a "single-cast" delegate is more about usage and convention, ensuring you point it to only one method at a time.