- How do you handle null reference exceptions in C#?
- Explain the concept of delegates in C#.
- What is a multicast delegate?
- What is an event in C#?
- Explain the difference between events and delegates.
Handling Null Reference Exceptions in C#:
Null Reference Exceptions occur when you try to access a member or method of an object that is null. To handle these exceptions, you can use conditional statements to check for null values before accessing members or methods, or you can use the null-conditional operator (?.
) introduced in C# 6.0.
// Using conditional statements if (obj != null) { obj.Method(); } // Using null-conditional operator obj?.Method();
In both cases, the method Method()
is only called if obj
is not null, preventing a Null Reference Exception.
Delegates in C#:
Delegates in C# are reference types that hold references to methods, allowing methods to be passed as arguments to other methods or stored as data. They enable a form of function pointer or callback mechanism, facilitating encapsulation and loose coupling in applications.
delegate void MyDelegate(string message); class Program { static void Main() { MyDelegate delegateObj = new MyDelegate(PrintMessage); delegateObj("Hello, World!"); } static void PrintMessage(string message) { Console.WriteLine(message); } }
In this example, MyDelegate
is a delegate type that can hold references to methods with a signature matching void methodName(string)
. The delegate is instantiated with the PrintMessage
method and invoked with a message string.
Multicast Delegate:
A multicast delegate in C# is a delegate that holds references to multiple methods. When invoked, a multicast delegate invokes all the methods it holds in the order they were added. Multicast delegates are commonly used in event handling scenarios.
delegate void MyDelegate(); class Program { static void Main() { MyDelegate delegateObj = Method1; delegateObj += Method2; delegateObj(); } static void Method1() { Console.WriteLine("Method 1"); } static void Method2() { Console.WriteLine("Method 2"); } }
In this example, delegateObj
is a multicast delegate that holds references to both Method1
and Method2
. When invoked, it calls both methods sequentially.
Event in C#:
An event in C# is a language construct that allows a class to provide notifications to other classes when certain actions occur. Events are based on delegates and follow the publisher-subscriber pattern, where one class (the publisher) raises events and other classes (subscribers) handle them.
class Button { public event EventHandler Click; public void OnClick() { Click?.Invoke(this, EventArgs.Empty); } }
In this example, the Click
event is declared in the Button
class. Subscribers can attach event handlers to the Click event and be notified when the button is clicked by calling the OnClick
method.
Difference between Events and Delegates:
- Delegates: Delegates are types that hold references to methods. They enable method invocation through delegate instances.
- Events: Events are a higher-level language construct built on top of delegates. They provide a standardized way for classes to provide notifications of state changes or actions to other classes without exposing their internal implementation.
In summary, delegates are the building blocks for events, while events provide a more structured and encapsulated way to implement the observer pattern in C#.
Comments
Post a Comment