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

Multicast delegates in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multicast Delegate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Multicast Delegate Invocation
What is the output of the following C# code using multicast delegates?
C Sharp (C#)
using System;

public delegate void Notify();

class Program
{
    static void Main()
    {
        Notify notify = MethodA;
        notify += MethodB;
        notify();
    }

    static void MethodA() => Console.WriteLine("MethodA called");
    static void MethodB() => Console.WriteLine("MethodB called");
}
ACompilation error
BMethodB called\nMethodA called
CMethodA called\nMethodB called
DMethodA called
Attempts:
2 left
💡 Hint
Multicast delegates invoke methods in the order they were added.
Predict Output
intermediate
2:00remaining
Return value of multicast delegate with int return type
What is the output of this C# code that uses a multicast delegate with an int return type?
C Sharp (C#)
using System;

public delegate int Compute(int x);

class Program
{
    static void Main()
    {
        Compute compute = Square;
        compute += Double;
        int result = compute(3);
        Console.WriteLine(result);
    }

    static int Square(int x) => x * x;
    static int Double(int x) => x * 2;
}
A6
B9
C15
DCompilation error
Attempts:
2 left
💡 Hint
Multicast delegates with return values return the last method's result.
Predict Output
advanced
2:00remaining
Effect of removing a method from a multicast delegate
What is the output of this C# program after removing a method from a multicast delegate?
C Sharp (C#)
using System;

public delegate void Notify();

class Program
{
    static void Main()
    {
        Notify notify = MethodA;
        notify += MethodB;
        notify -= MethodA;
        notify();
    }

    static void MethodA() => Console.WriteLine("MethodA called");
    static void MethodB() => Console.WriteLine("MethodB called");
}
AMethodB called
BMethodA called\nMethodB called
CMethodA called
DNo output
Attempts:
2 left
💡 Hint
Removing a method unsubscribes it from the delegate invocation list.
Predict Output
advanced
2:00remaining
Exception behavior in multicast delegates
What happens when a multicast delegate invokes multiple methods and one throws an exception?
C Sharp (C#)
using System;

public delegate void Notify();

class Program
{
    static void Main()
    {
        Notify notify = MethodA;
        notify += MethodB;
        try
        {
            notify();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught: " + ex.Message);
        }
    }

    static void MethodA() => Console.WriteLine("MethodA called");
    static void MethodB() => throw new InvalidOperationException("Error in MethodB");
}
AMethodA called\nMethodB called
BMethodA called\nException caught: Error in MethodB
CException caught: Error in MethodB
DNo output
Attempts:
2 left
💡 Hint
Multicast delegates invoke methods in order until an exception occurs.
🧠 Conceptual
expert
2:00remaining
Number of methods in a multicast delegate
Given the following code, how many methods are in the multicast delegate 'notify' before invocation?
C Sharp (C#)
using System;

public delegate void Notify();

class Program
{
    static void Main()
    {
        Notify notify = MethodA;
        notify += MethodB;
        notify += MethodC;
        notify -= MethodB;
        notify += MethodD;
    }

    static void MethodA() { }
    static void MethodB() { }
    static void MethodC() { }
    static void MethodD() { }
}
A1
B4
C2
D3
Attempts:
2 left
💡 Hint
Count additions and removals carefully.