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

Delegates as callback pattern in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delegate Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of delegate callback invocation

What is the output of this C# program that uses a delegate as a callback?

C Sharp (C#)
using System;

public delegate void Notify(string message);

class Program
{
    static void Main()
    {
        Notify notifier = PrintMessage;
        notifier("Hello from delegate!");
    }

    static void PrintMessage(string msg)
    {
        Console.WriteLine(msg);
    }
}
AHello from delegate!
BSystem.Delegate
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint

Look at how the delegate is assigned and invoked.

Predict Output
intermediate
2:00remaining
Delegate callback with multiple methods

What will be the output of this C# program using a multicast delegate as a callback?

C Sharp (C#)
using System;

public delegate void Notify(string message);

class Program
{
    static void Main()
    {
        Notify notifier = PrintMessage;
        notifier += PrintMessageUpper;
        notifier("Hello!");
    }

    static void PrintMessage(string msg)
    {
        Console.WriteLine(msg);
    }

    static void PrintMessageUpper(string msg)
    {
        Console.WriteLine(msg.ToUpper());
    }
}
AHELLO!\nHello!
BCompile-time error
CHello!
DHello!\nHELLO!
Attempts:
2 left
💡 Hint

Multicast delegates call all methods in their invocation list.

🔧 Debug
advanced
2:00remaining
Identify the runtime error in delegate callback

What error does this C# program produce when run?

C Sharp (C#)
using System;

public delegate void Notify(string message);

class Program
{
    static void Main()
    {
        Notify notifier = null;
        notifier("Test message");
    }
}
ANullReferenceException
BCompile-time error: delegate not assigned
CArgumentNullException
DNo error, prints 'Test message'
Attempts:
2 left
💡 Hint

What happens if you call a delegate variable that is null?

🧠 Conceptual
advanced
2:00remaining
Delegate callback pattern usage

Which statement best describes the use of delegates as callbacks in C#?

ADelegates automatically run all methods in parallel threads.
BDelegates allow methods to be passed as parameters and called later, enabling flexible callback patterns.
CDelegates are used only to store multiple unrelated methods without parameters.
DDelegates replace all event handling mechanisms in C#.
Attempts:
2 left
💡 Hint

Think about what delegates enable in terms of method references and callbacks.

📝 Syntax
expert
2:00remaining
Correct delegate declaration for callback with return value

Which option correctly declares a delegate type for a callback that takes an int and returns a string?

Adelegate string Callback(int value) {}
Bpublic delegate void Callback(int value) => string;
Cpublic delegate string Callback(int value);
Dpublic Callback delegate string(int value);
Attempts:
2 left
💡 Hint

Remember the syntax for declaring delegates: public delegate ReturnType Name(ParameterList);