0
0
C Sharp (C#)programming~10 mins

Raising events safely in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an event named OnChange.

C Sharp (C#)
public event EventHandler [1];
Drag options to blanks, or click blank then click option'
AEventChanged
BChanged
CChangeEvent
DOnChange
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different event name than declared.
Forgetting to declare the event type.
2fill in blank
medium

Complete the code to safely raise the event OnChange using a local copy.

C Sharp (C#)
var handler = [1];
if (handler != null)
{
    handler(this, EventArgs.Empty);
}
Drag options to blanks, or click blank then click option'
AOnChange
BHandler
ConChange
DChangeEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the event name.
Not checking for null before invoking the event.
3fill in blank
hard

Fix the error in the event invocation by completing the code.

C Sharp (C#)
OnChange[1](this, EventArgs.Empty);
Drag options to blanks, or click blank then click option'
A.Invoke
B()
C.Call
D.Raise
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the event like a method without .Invoke.
Using non-existent methods like .Raise or .Call.
4fill in blank
hard

Fill both blanks to create a method RaiseOnChange that safely raises the event.

C Sharp (C#)
protected virtual void RaiseOnChange()
{
    var handler = [1];
    if (handler [2] null)
        handler(this, EventArgs.Empty);
}
Drag options to blanks, or click blank then click option'
AOnChange
B==
C!=
DOnChanged
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != in the if condition.
Copying the wrong event name.
5fill in blank
hard

Fill all three blanks to raise the event with a custom EventArgs subclass MyEventArgs.

C Sharp (C#)
protected virtual void RaiseOnChange(MyEventArgs e)
{
    var handler = [1];
    if (handler != null)
        handler([2], [3]);
}
Drag options to blanks, or click blank then click option'
AOnChange
Bthis
Ce
DEventArgs.Empty
Attempts:
3 left
💡 Hint
Common Mistakes
Passing EventArgs.Empty instead of the custom e.
Not checking if the event is null before invoking.