Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a custom event argument class inheriting from EventArgs.
C Sharp (C#)
public class MyEventArgs : [1] { public int Value { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using EventHandler instead of EventArgs as base class.
Trying to inherit from a non-existent class like EventArgsBase.
✗ Incorrect
Custom event argument classes should inherit from EventArgs to be used in events.
2fill in blank
mediumComplete the code to declare an event using the custom event argument class.
C Sharp (C#)
public event EventHandler<[1]> OnValueChanged; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base EventArgs instead of the custom class.
Using a class name that was not declared.
✗ Incorrect
The event uses the custom event argument class MyEventArgs as the generic parameter.
3fill in blank
hardFix the error in the event invocation by completing the code.
C Sharp (C#)
OnValueChanged?.Invoke(this, new [1] { Value = 10 });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using EventArgs instead of MyEventArgs when invoking the event.
Trying to invoke the event with EventHandler instead of event args.
✗ Incorrect
The event must be invoked with an instance of the custom event argument class MyEventArgs.
4fill in blank
hardFill both blanks to complete the event handler method signature.
C Sharp (C#)
private void [1](object [2], MyEventArgs e) { Console.WriteLine($"Value changed to {e.Value}"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid method names.
Using incorrect parameter names that do not match event handler conventions.
✗ Incorrect
The method name can be any valid identifier, here OnValueChangedHandler, and the sender parameter is conventionally named sender.
5fill in blank
hardFill all three blanks to subscribe to the event and trigger it.
C Sharp (C#)
MyClass obj = new MyClass(); obj.[1] += [2]; obj.TriggerEvent(); // Event handler method private void [3](object sender, MyEventArgs e) { Console.WriteLine($"New value: {e.Value}"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching event and handler names.
Using incorrect event names that do not exist.
✗ Incorrect
Subscribe to the event OnValueChanged with the handler OnValueChangedHandler, which is defined below.