Introduction
Delegates and Events are very important concepts in C#. They are mainly used in event-driven programming.
Delegates allow methods to be passed as parameters, while Events allow a class to notify other classes when something happens.
These concepts are widely used in Windows applications, ASP.NET applications, and game development.
1. What is a Delegate in C#?
A delegate in C# is a type-safe function pointer that holds the reference to a method.
It allows methods to be passed as parameters and enables callback functionality.
Delegates are mainly used in event handling and asynchronous programming.
2. Why are Delegates used in C#?
Delegates are used to:
- Pass methods as parameters
- Implement callback methods
- Create flexible and reusable code
- Implement events
3. What is the syntax of a delegate?
public delegate void MyDelegate(string message);
4. What is a delegate instance?
A delegate instance is an object that refers to a method with the same signature as the delegate.
5. What is a delegate signature?
The method that a delegate references must have the same return type and parameters as the delegate definition.
6. Can a delegate call multiple methods?
Yes, using multicast delegates a delegate can reference multiple methods.
7. What is a Multicast Delegate?
A multicast delegate is a delegate that can hold references to more than one method.
When the delegate is invoked, all methods are executed sequentially.
8. How do you create a multicast delegate?
delegate void Print(); Print p = Method1; p += Method2;
9. What is the difference between delegate and function pointer?
Delegates are type-safe and secure, while traditional function pointers are not type-safe.
10. What are the advantages of delegates?
- Type safety
- Encapsulation
- Flexibility
- Support for callback functions
11. What is a Singlecast Delegate?
A delegate that references only one method is called a singlecast delegate.
12. What is Delegate Invocation?
Calling a delegate to execute the referenced method is called delegate invocation.
13. What happens if a delegate references multiple methods?
All methods are executed in the order they were added.
14. Can delegates return values?
Yes, delegates can return values depending on their return type.
15. What is Anonymous Method in Delegates?
Anonymous methods allow defining a method inline without declaring a separate method.
16. Example of anonymous method
delegate void Print();
Print p = delegate() {
Console.WriteLine("Hello");
};
17. What is Lambda Expression in delegates?
Lambda expressions are a shorter syntax for anonymous methods.
18. Example of Lambda delegate
Print p = () => Console.WriteLine("Hello World");
19. What is Func delegate?
Func is a predefined generic delegate that returns a value.
20. Example of Func delegate
Func<int,int,int> add = (a,b) => a+b;
21. What is Action delegate?
Action is a generic delegate that does not return a value.
22. Example of Action delegate
Action print = s => Console.WriteLine(s);
23. What is Predicate delegate?
Predicate is a delegate that returns a boolean value.
24. Example of Predicate delegate
Predicate isEven = x => x % 2 == 0;
25. Can delegates be used with methods of different classes?
Yes, as long as the method signature matches the delegate signature.
26. Can delegates reference static methods?
Yes, delegates can reference both static and instance methods.
27. What is delegate chaining?
Combining multiple methods with a delegate using += operator.
28. How to remove a method from delegate?
p -= Method1;
29. Are delegates reference types?
Yes, delegates are reference types derived from System.Delegate class.
30. What namespace contains delegate classes?
System namespace.
31. Can delegates be passed as parameters?
Yes, delegates are commonly passed as parameters to methods.
32. What is callback method?
A method that is passed as a delegate and executed later is called a callback method.
33. Can delegates reference constructors?
No, delegates cannot reference constructors.
34. What is Delegate Combine?
It combines two delegates into a single multicast delegate.
35. What is Delegate Remove?
It removes a method from the delegate invocation list.
36. Can delegates be generic?
Yes, delegates can be defined using generic types.
37. Example of generic delegate
delegate T MyDelegate(T value);
38. What is delegate covariance?
It allows a method with a more derived return type to match a delegate.
39. What is delegate contravariance?
It allows a method with less derived parameter types.
40. Are delegates thread safe?
Delegate invocation itself is thread-safe but modification of invocation list requires care.
41. What is BeginInvoke?
Used to invoke delegates asynchronously.
42. What is EndInvoke?
Used to retrieve results of asynchronous delegate invocation.
43. What is delegate invocation list?
List of all methods referenced by the delegate.
44. How to view invocation list?
delegate.GetInvocationList();
45. What happens if one method throws exception?
Execution stops unless exception handling is used.
46. Can delegates be nested?
Yes, delegates can be declared inside classes.
47. What is delegate serialization?
Delegates can be serialized if all referenced methods are serializable.
48. Are delegates immutable?
Yes, delegates are immutable. Adding/removing creates a new delegate instance.
49. Can delegates be stored in collections?
Yes, delegates can be stored in arrays or lists.
50. What is the base class of delegates?
System.MulticastDelegate.
51. What is an Event in C#?
An event is a mechanism that allows a class to notify other classes when something happens.
Events are based on delegates.
52. Why are events used?
- Event-driven programming
- Loose coupling between objects
- Notification mechanism
53. Basic syntax of event
public event EventHandler MyEvent;
54. What is EventHandler?
EventHandler is a predefined delegate used for events.
55. What are the components of event model?
- Publisher
- Subscriber
- Event handler
56. What is Publisher?
The class that raises the event.
57. What is Subscriber?
The class that listens to the event.
58. What is Event Handler?
The method executed when the event occurs.
59. How to raise an event?
MyEvent?.Invoke(this, EventArgs.Empty);
60. What is EventArgs?
Base class for event data.
61. What is Custom EventArgs?
User-defined class derived from EventArgs to pass custom data.
62. Example of custom EventArgs
class MyEventArgs : EventArgs
{
public int Data { get; set; }
}
63. What is event accessor?
Used to control how subscribers add or remove event handlers.
64. Can events be static?
Yes, events can be declared static.
65. What is event keyword?
Used to declare an event in C#.
66. Can events return values?
No, events do not return values.
67. What is event subscription?
publisher.MyEvent += HandlerMethod;
68. What is event unsubscription?
publisher.MyEvent -= HandlerMethod;
69. What happens if no subscribers exist?
Event invocation should check for null.
70. What is event delegate?
The delegate type used by an event.
71. What is event encapsulation?
Events restrict direct invocation from outside class.
72. Can events be virtual?
Yes, events can be virtual.
73. What is event overriding?
Derived classes can override virtual events.
74. What is event hiding?
Using the new keyword to hide base class event.
75. What is event-driven programming?
Programming based on events such as mouse click, key press etc.
76. Where are events commonly used?
- Windows Forms
- WPF
- ASP.NET
- Game development
77. What is event delegate signature?
Most events use (object sender, EventArgs e) pattern.
78. What is sender parameter?
Represents the object that raised the event.
79. What is e parameter?
Contains event data.
80. Can multiple subscribers exist?
Yes, events support multiple subscribers.
81. What happens if event is not unsubscribed?
It may cause memory leaks.
82. What is event multicast?
Multiple handlers responding to same event.
83. Can events be private?
Yes, events can have access modifiers.
84. What is protected event?
Accessible in derived classes only.
85. What is event delegate inference?
Compiler automatically detects delegate type.
86. What is event pattern in .NET?
Standard pattern using EventHandler delegate.
87. What is strongly typed event?
Event using custom EventArgs.
88. Can events be asynchronous?
Yes, handlers can be asynchronous.
89. What is event bubbling?
Event propagation to parent elements (common in UI frameworks).
90. What is event routing?
Mechanism used in WPF for event propagation.
91. What is routed event?
Special type of event used in WPF.
92. What is direct event?
Handled only by the element that raised it.
93. What is tunneling event?
Event travels from parent to child.
94. What is bubbling event?
Event travels from child to parent.
95. Can events be abstract?
Yes, abstract events can be declared in abstract classes.
96. Can events be sealed?
Yes, to prevent overriding.
97. What is event delegate type safety?
Ensures handlers match event signature.
98. Can lambda be used as event handler?
button.Click += (s,e) => Console.WriteLine("Clicked");
99. What is event lifecycle?
Event declaration → subscription → event raising → handler execution.
100. Difference between Delegate and Event?
| Delegate | Event |
|---|---|
| Represents method reference | Represents notification |
| Can be invoked anywhere | Invoked only inside declaring class |
| General purpose | Used for event handling |
📢 Join Our WhatsApp Channel
💼 Get Daily IT Job Updates, Interview Preparation Tips & Instant Alerts directly on WhatsApp.
👉 Join WhatsApp Now📢 Join Our Telegram Channel
💼 Get Daily IT Job Updates, Interview Tips & Exclusive Alerts directly on Telegram!
👉 Join Telegram