Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Event declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Event Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this event declaration and usage?

Consider the following C# code snippet. What will be printed when the program runs?

C Sharp (C#)
using System;

class Program {
    public delegate void Notify();
    public event Notify OnNotify;

    public void Trigger() {
        OnNotify?.Invoke();
    }

    static void Main() {
        Program p = new Program();
        p.OnNotify += () => Console.WriteLine("Event triggered!");
        p.Trigger();
    }
}
ARuntime error: NullReferenceException
BNo output
CCompile-time error: event must be declared inside a class
DEvent triggered!
Attempts:
2 left
💡 Hint

Check if the event is subscribed before invoking.

❓ Predict Output
intermediate
2:00remaining
What error occurs with this event declaration?

What error will this code produce?

C Sharp (C#)
using System;

class Program {
    public event Action OnAction = () => {};

    static void Main() {
        Program p = new Program();
        p.OnAction();
    }
}
ARuntime error: NullReferenceException
BCompile-time error: Events cannot have initializers
CNo output, runs fine
DCompile-time error: Missing delegate declaration
Attempts:
2 left
💡 Hint

Events cannot be assigned directly with initial values.

🔧 Debug
advanced
2:00remaining
Why does this event invocation cause a runtime error?

Examine the code below. Why does it throw an exception at runtime?

C Sharp (C#)
using System;

class Program {
    public event Action OnEvent;

    public void Raise() {
        OnEvent();
    }

    static void Main() {
        Program p = new Program();
        p.Raise();
    }
}
ANullReferenceException because OnEvent has no subscribers
BCompile-time error: event cannot be invoked outside the declaring class
CNo output, runs fine
DInvalidOperationException because event is null
Attempts:
2 left
💡 Hint

Check if the event is null before invoking.

📝 Syntax
advanced
2:00remaining
Which option correctly declares an event with a custom delegate?

Which of the following is the correct way to declare an event using a custom delegate in C#?

A
delegate public void MyEventHandler(string message);
event MyEventHandler EventOccurred;
B
public event delegate void MyEventHandler(string message);
public EventOccurred MyEventHandler;
C
public delegate void MyEventHandler(string message);
public event MyEventHandler EventOccurred;
D
public void delegate MyEventHandler(string message);
public event EventOccurred MyEventHandler;
Attempts:
2 left
💡 Hint

Remember the order: delegate declaration first, then event declaration.

🚀 Application
expert
2:00remaining
How many subscribers does the event have after this code runs?

Given the following code, how many methods are subscribed to the OnChange event after execution?

C Sharp (C#)
using System;

class Program {
    public event Action OnChange;

    public void Subscribe() {
        OnChange += Handler1;
        OnChange += Handler2;
        OnChange -= Handler1;
        OnChange += Handler3;
    }

    void Handler1() { }
    void Handler2() { }
    void Handler3() { }

    static void Main() {
        Program p = new Program();
        p.Subscribe();
        Console.WriteLine(p.OnChange?.GetInvocationList().Length ?? 0);
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Consider the += and -= operations on the event.

Practice

(1/5)
1. What keyword is used in C# to declare an event?
easy
A. trigger
B. delegate
C. event
D. notify

Solution

  1. Step 1: Understand event declaration basics

    In C#, events are declared using the event keyword followed by a delegate type.
  2. Step 2: Identify the correct keyword

    The keyword event specifically marks a member as an event, enabling subscription and notification.
  3. Final Answer:

    event -> Option C
  4. Quick Check:

    Event declaration uses 'event' keyword [OK]
Hint: Events always start with the 'event' keyword in C# [OK]
Common Mistakes:
  • Using 'delegate' instead of 'event' keyword
  • Confusing event with method declaration
  • Trying to declare event without a delegate type
2. Which of the following is the correct syntax to declare an event named OnClick using a delegate Action?
easy
A. public event Action OnClick;
B. public delegate event Action OnClick;
C. event public Action OnClick();
D. public Action event OnClick;

Solution

  1. Step 1: Recall correct event declaration syntax

    The correct syntax is: public event DelegateType EventName; without extra keywords or parentheses.
  2. Step 2: Analyze each option

    public event Action OnClick; matches the correct syntax exactly. Options A, B, and D misuse keyword order or add invalid syntax.
  3. Final Answer:

    public event Action OnClick; -> Option A
  4. Quick Check:

    Correct event syntax = public event Action OnClick; [OK]
Hint: Remember: 'event' comes before delegate type, no parentheses [OK]
Common Mistakes:
  • Adding parentheses after event name
  • Placing 'event' after delegate type
  • Combining 'delegate' and 'event' keywords incorrectly
3. What will be the output of the following code?
using System;
class Program {
public static event Action OnStart;
static void Main() {
OnStart += () => Console.WriteLine("Started");
OnStart?.Invoke();
}
}
medium
A. No output
B. Compilation error
C. Runtime error
D. Started

Solution

  1. Step 1: Understand event subscription and invocation

    The event OnStart is subscribed with a lambda that prints "Started". The ?.Invoke() safely calls the event if not null.
  2. Step 2: Predict program output

    Since the event has one subscriber, invoking it prints "Started" to the console.
  3. Final Answer:

    Started -> Option D
  4. Quick Check:

    Subscribed event invoked prints 'Started' [OK]
Hint: ?.Invoke calls event only if not null, so output prints [OK]
Common Mistakes:
  • Assuming event is null and no output
  • Confusing event invocation syntax
  • Expecting compilation or runtime errors
4. Identify the error in the following event declaration and fix it:
public class Button {
public event Action OnClick();
}
medium
A. Change 'event' to 'delegate'
B. Remove parentheses after OnClick
C. Add 'void' before event keyword
D. Add parentheses after event keyword

Solution

  1. Step 1: Analyze event declaration syntax

    Events are declared without parentheses after the event name because they are not methods but event fields.
  2. Step 2: Correct the syntax

    Removing parentheses after OnClick fixes the syntax: public event Action OnClick;
  3. Final Answer:

    Remove parentheses after OnClick -> Option B
  4. Quick Check:

    Events declared without parentheses [OK]
Hint: Events are fields, not methods--no parentheses after name [OK]
Common Mistakes:
  • Adding parentheses after event name
  • Using 'delegate' keyword instead of 'event'
  • Trying to declare event as a method
5. You want to declare an event DataReceived that uses a custom delegate DataHandler accepting a string parameter. Which is the correct declaration?
hard
A. public delegate void DataHandler(string data); public event DataHandler DataReceived;
B. public event void DataHandler(string data); public DataReceived event;
C. public event DataHandler(string data) DataReceived;
D. public DataHandler event DataReceived(string data);

Solution

  1. Step 1: Declare the delegate type first

    Custom delegates must be declared separately with the correct signature: public delegate void DataHandler(string data);
  2. Step 2: Declare the event using the delegate type

    The event is declared as: public event DataHandler DataReceived; without parentheses.
  3. Final Answer:

    public delegate void DataHandler(string data); public event DataHandler DataReceived; -> Option A
  4. Quick Check:

    Delegate declared first, event uses delegate type [OK]
Hint: Declare delegate first, then event with delegate type and no parentheses [OK]
Common Mistakes:
  • Trying to declare event with parameters directly
  • Mixing delegate and event keywords
  • Adding parentheses after event name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard