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

Why events are needed in C Sharp (C#) - Test Your Understanding

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

Complete the code to declare an event in C#.

C Sharp (C#)
public event EventHandler [1];
Drag options to blanks, or click blank then click option'
AClick
BOnClick
CClicked
DClickEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using method-like names with 'On' prefix for the event declaration.
Using past tense verbs for event names.
2fill in blank
medium

Complete the code to subscribe to an event named Click.

C Sharp (C#)
button.Click [1]= Button_ClickHandler;
Drag options to blanks, or click blank then click option'
A+
B-
C+=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '+='.
Using '==' which is a comparison operator.
3fill in blank
hard

Fix the error in the event handler signature for the Click event.

C Sharp (C#)
private void Button_ClickHandler(object [1], EventArgs e) { /* handler code */ }
Drag options to blanks, or click blank then click option'
Aobj
Bsource
CeventSource
Dsender
Attempts:
3 left
💡 Hint
Common Mistakes
Using arbitrary parameter names that confuse the event source.
Using names that don't clearly indicate the sender.
4fill in blank
hard

Fill both blanks to raise an event safely in C#.

C Sharp (C#)
EventHandler handler = [1];
if (handler [2] null) handler(this, EventArgs.Empty);
Drag options to blanks, or click blank then click option'
AClick
B!=
C==
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if handler == null instead of != null.
Using wrong variable names.
5fill in blank
hard

Fill all three blanks to declare, subscribe, and raise an event named DataReceived.

C Sharp (C#)
public event EventHandler [1];

void Subscribe() {
    [2] += OnDataReceived;
}

void Raise() {
    [3] handler = [2];
    if (handler != null) handler(this, EventArgs.Empty);
}
Drag options to blanks, or click blank then click option'
ADataReceived
BEventHandler
Cvar
DEventArgs
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the event in different places.
Using incorrect types for the handler variable.