C# Interview Questions
C# Programming
Web DevelopmentFrontendBackendGame DevQuestion 11
What are delegates in C#?
Answer:
Delegates in C# are a type that represents references to methods with a specific parameter list and return type. They are similar to function pointers in C and C++, but are type-safe and secure. Delegates are used to pass methods as arguments to other methods, define callback methods, and implement event handling.
Key Features of Delegates
-
Type Safety:
- Delegates are type-safe, meaning they ensure that the signature of the method being called matches the signature defined by the delegate.
-
Flexibility:
- Delegates can reference both static and instance methods.
- They can point to methods that have the same signature.
-
Multicasting:
- Delegates can hold references to more than one method at a time. This is called multicasting.
- When a multicast delegate is invoked, all the methods it references are called in sequence.
Declaring and Using Delegates
Declaration
A delegate is declared using the delegate
keyword followed by a signature that includes the return type and parameters.
public delegate void MyDelegate(string message);
Instantiation and Invocation
To use a delegate, you create an instance of the delegate, assign it a method, and then invoke it.
public class Program
{
// Method that matches the delegate signature
public static void DisplayMessage(string message)
{
Console.WriteLine(message);
}
public static void Main()
{
// Instantiate the delegate
MyDelegate del = new MyDelegate(DisplayMessage);
// Invoke the delegate
del("Hello, World!");
}
}
Multicast Delegates
Multicast delegates can reference multiple methods. When invoked, they call all the methods in their invocation list.
public class Program
{
public static void DisplayMessage(string message)
{
Console.WriteLine(message);
}
public static void LogMessage(string message)
{
// Simulate logging
Console.WriteLine($"Log: {message}");
}
public static void Main()
{
MyDelegate del = new MyDelegate(DisplayMessage);
del += LogMessage; // Adding another method to the delegate
del("Hello, World!"); // Both DisplayMessage and LogMessage will be called
}
}
Use Cases for Delegates
-
Event Handling:
- Delegates are the foundation of event handling in C#. Events use delegates to signal state changes or user actions.
-
Callbacks:
- Delegates are used to implement callback methods, which are methods passed as arguments to other methods to be called at a later time.
-
Encapsulation of Method Calls:
- Delegates can encapsulate method calls, allowing methods to be passed around and invoked dynamically.
Example: Event Handling with Delegates
public class Button
{
// Declare a delegate for the event
public delegate void ClickHandler(string message);
// Declare an event of the delegate type
public event ClickHandler Click;
// Method to trigger the event
public void OnClick(string message)
{
if (Click != null)
{
Click(message);
}
}
}
public class Program
{
// Event handler method
public static void Button_Click(string message)
{
Console.WriteLine($"Button clicked: {message}");
}
public static void Main()
{
Button button = new Button();
// Subscribe to the event
button.Click += Button_Click;
// Trigger the event
button.OnClick("Hello, World!");
}
}
Summary
Delegates in C# are powerful tools that enable methods to be treated as first-class objects, allowing for flexible and dynamic method invocation. They provide a way to encapsulate method references, support event handling, and facilitate callback mechanisms, making them essential for advanced C# programming.