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

Why delegates are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delegate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of delegate invocation
What is the output of this C# code that uses a delegate to call a method?
C Sharp (C#)
using System;

public delegate void Notify(string message);

class Program {
    static void Main() {
        Notify notify = ShowMessage;
        notify("Hello, delegate!");
    }

    static void ShowMessage(string msg) {
        Console.WriteLine(msg);
    }
}
AHello, delegate!
BShowMessage
CCompile-time error
DRuntime exception
Attempts:
2 left
💡 Hint
Look at how the delegate calls the method ShowMessage with a string argument.
🧠 Conceptual
intermediate
1:30remaining
Why use delegates in C#?
Which of these best explains why delegates are needed in C#?
ATo create variables that hold numbers only.
BTo replace all classes with functions.
CTo store data in a list.
DTo allow methods to be passed as parameters and enable flexible callback mechanisms.
Attempts:
2 left
💡 Hint
Think about how you might want to call different methods dynamically.
Predict Output
advanced
2:30remaining
Multicast delegate output
What will be the output of this C# program using a multicast delegate?
C Sharp (C#)
using System;

public delegate void Notify();

class Program {
    static void Main() {
        Notify notify = First;
        notify += Second;
        notify();
    }

    static void First() {
        Console.WriteLine("First called");
    }

    static void Second() {
        Console.WriteLine("Second called");
    }
}
AFirst called\nSecond called
BCompile-time error
CFirst called
DSecond called\nFirst called
Attempts:
2 left
💡 Hint
Multicast delegates call all methods in the order they were added.
Predict Output
advanced
2:30remaining
Delegate with return value behavior
What is the output of this C# code where a delegate has multiple methods with return values?
C Sharp (C#)
using System;

public delegate int Calculate(int x, int y);

class Program {
    static void Main() {
        Calculate calc = Add;
        calc += Multiply;
        int result = calc(3, 4);
        Console.WriteLine(result);
    }

    static int Add(int a, int b) {
        return a + b;
    }

    static int Multiply(int a, int b) {
        return a * b;
    }
}
A7\n12
B7
C12
DCompile-time error
Attempts:
2 left
💡 Hint
When a multicast delegate returns a value, only the last method's return is used.
🧠 Conceptual
expert
2:00remaining
Delegate use in event handling
Why are delegates essential for event handling in C#?
ABecause they replace the need for classes in event-driven programs.
BBecause they allow methods to be assigned and called dynamically when events occur.
CBecause they store event data in variables.
DBecause they automatically generate user interface elements.
Attempts:
2 left
💡 Hint
Think about how events notify multiple methods when something happens.