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

Publisher-subscriber execution model in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Publisher-Subscriber Master
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 simple publisher-subscriber example?

Consider the following C# code implementing a basic publisher-subscriber pattern. What will be printed when the program runs?

C Sharp (C#)
using System;

public class Publisher
{
    public event Action OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke();
    }
}

public class Subscriber
{
    public void Respond()
    {
        Console.WriteLine("Subscriber received event.");
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        Subscriber sub = new Subscriber();
        pub.OnChange += sub.Respond;
        pub.RaiseEvent();
    }
}
ASubscriber received event.
BNo output
CRuntime error: NullReferenceException
DCompile error: event invocation not allowed
Attempts:
2 left
💡 Hint

Look at how the event is subscribed and invoked.

Predict Output
intermediate
2:00remaining
What happens if no subscriber is attached?

What will be the output or behavior of this code when RaiseEvent is called but no subscriber is attached?

C Sharp (C#)
using System;

public class Publisher
{
    public event Action OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke();
        Console.WriteLine("Event raised.");
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        pub.RaiseEvent();
    }
}
ACompile error: event invocation not allowed
BRuntime error: NullReferenceException
CNo output
DEvent raised.
Attempts:
2 left
💡 Hint

Check the null-conditional operator usage.

🔧 Debug
advanced
2:00remaining
Identify the error in this publisher-subscriber code

What error will this code produce when compiled or run?

C Sharp (C#)
using System;

public class Publisher
{
    public event Action OnChange;

    public void RaiseEvent()
    {
        OnChange();
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        pub.RaiseEvent();
    }
}
ARuntime error: NullReferenceException
BCompile error: event invocation not allowed
CNo output
DRuntime error: StackOverflowException
Attempts:
2 left
💡 Hint

Consider what happens if the event has no subscribers and is invoked directly.

🧠 Conceptual
advanced
2:00remaining
Which statement about publisher-subscriber events is true?

Choose the correct statement about the publisher-subscriber pattern using C# events.

AAn event can be invoked from outside the class that declares it.
BSubscribers can only attach methods with matching delegate signatures.
CEvents automatically queue messages if subscribers are busy.
DMultiple events cannot share the same delegate type.
Attempts:
2 left
💡 Hint

Think about how delegates and events work in C#.

Predict Output
expert
3:00remaining
What is the output when multiple subscribers are attached?

Given this code with two subscribers attached to the same event, what will be printed?

C Sharp (C#)
using System;

public class Publisher
{
    public event Action OnChange;

    public void RaiseEvent()
    {
        OnChange?.Invoke();
    }
}

public class Program
{
    public static void Main()
    {
        Publisher pub = new Publisher();
        pub.OnChange += () => Console.WriteLine("First subscriber called.");
        pub.OnChange += () => Console.WriteLine("Second subscriber called.");
        pub.RaiseEvent();
    }
}
ANo output
BFirst subscriber called.
C
First subscriber called.
Second subscriber called.
D
Second subscriber called.
First subscriber called.
Attempts:
2 left
💡 Hint

Events invoke subscribers in the order they were added.