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

Event-driven design pattern 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 in C#.

C Sharp (C#)
public event EventHandler [1];
Drag options to blanks, or click blank then click option'
AOnClick
BClicked
CClick
DClickEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using method-like names such as 'OnClick' instead of event names.
Using past tense like 'Clicked' which is less common for event names.
2fill in blank
medium

Complete the code to subscribe to an event named 'DataReceived'.

C Sharp (C#)
someObject.DataReceived [1]= HandlerMethod;
Drag options to blanks, or click blank then click option'
A+=
B==
C+
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which overwrites existing handlers.
Using '+' which is invalid syntax for event subscription.
3fill in blank
hard

Fix the error in the event invocation code.

C Sharp (C#)
if ([1] != null)
{
    [1](this, EventArgs.Empty);
}
Drag options to blanks, or click blank then click option'
ADataReceived
BDataReceived()
CDataReceived?.Invoke
DDataReceived.Invoke
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses in the null check condition.
Using the null-conditional operator '?.Invoke' inside the if condition.
4fill in blank
hard

Fill both blanks to declare and raise a custom event with EventHandler.

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

protected virtual void On[2](EventArgs e)
{
    [1]?.Invoke(this, e);
}
Drag options to blanks, or click blank then click option'
ADataProcessed
BDataProcessedEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for event and method.
Adding 'Event' suffix inconsistently.
5fill in blank
hard

Fill all three blanks to create a custom event with a delegate and raise it.

C Sharp (C#)
public delegate void [1](object sender, EventArgs e);

public event [1] [2];

protected virtual void On[2](EventArgs e)
{
    [2]?.Invoke(this, e);
}
Drag options to blanks, or click blank then click option'
ADataChangedHandler
BDataChanged
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing delegate and event names.
Not matching the method name with the event name.