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

Publisher-subscriber execution model 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 a delegate named Notify.

C Sharp (C#)
public delegate void [1]();
Drag options to blanks, or click blank then click option'
ANotify
BEventHandler
CSubscriber
DPublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of a delegate name.
Using a built-in delegate name incorrectly.
2fill in blank
medium

Complete the code to declare an event named OnNotify using the Notify delegate.

C Sharp (C#)
public event [1] OnNotify;
Drag options to blanks, or click blank then click option'
ANotify
BEventHandler
CAction
DEventArgs
Attempts:
3 left
💡 Hint
Common Mistakes
Using a delegate type that does not match the event signature.
Using a built-in delegate instead of the custom one.
3fill in blank
hard

Fix the error in the code to correctly subscribe a method named HandleNotification to the OnNotify event.

C Sharp (C#)
publisher.OnNotify [1]= HandleNotification;
Drag options to blanks, or click blank then click option'
A-
B+=
C==
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '+='.
Using '-' or '==' operators incorrectly.
4fill in blank
hard

Fill both blanks to raise the OnNotify event safely.

C Sharp (C#)
if (OnNotify [1] null)
{
    OnNotify[2]();
}
Drag options to blanks, or click blank then click option'
A!=
B==
C()
D?.Invoke
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the condition.
Calling the event directly without null check.
5fill in blank
hard

Fill all three blanks to create a dictionary of subscribers with their names and subscribe them to the event.

C Sharp (C#)
var subscribers = new Dictionary<string, Notify>()
{
    {"Alice", [1],
    {"Bob", [2]
};

foreach (var subscriber in subscribers)
{
    publisher.OnNotify [3]= subscriber.Value;
}
Drag options to blanks, or click blank then click option'
AAliceHandler
BBobHandler
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operator to subscribe or unsubscribe.
Using incorrect method names for subscribers.