Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a multicast delegate in C#?
easy
A. To call multiple methods with a single delegate invocation
B. To store only one method reference at a time
C. To create a delegate that returns multiple values
D. To prevent methods from being called more than once

Solution

  1. Step 1: Understand delegate basics

    A delegate holds references to methods and can invoke them.
  2. Step 2: Identify multicast delegate behavior

    Multicast delegates can hold multiple method references and call them all in order.
  3. Final Answer:

    To call multiple methods with a single delegate invocation -> Option A
  4. Quick Check:

    Multicast delegates call many methods [OK]
Hint: Multicast delegates call many methods at once [OK]
Common Mistakes:
  • Thinking multicast delegates return multiple values
  • Confusing with single method delegates
  • Assuming multicast delegates prevent repeated calls
2. Which syntax correctly adds a method named ShowMessage to a multicast delegate del?
easy
A. del += ShowMessage;
B. del -+ ShowMessage;
C. del =+ ShowMessage;
D. del = ShowMessage;

Solution

  1. Step 1: Recall delegate addition syntax

    To add a method to a multicast delegate, use the '+=' operator.
  2. Step 2: Check each option

    del += ShowMessage; uses 'del += ShowMessage;' which is correct syntax to add a method.
  3. Final Answer:

    del += ShowMessage; -> Option A
  4. Quick Check:

    Use '+=' to add methods to multicast delegates [OK]
Hint: Use '+=' to add methods to multicast delegates [OK]
Common Mistakes:
  • Using '=' instead of '+=' to add methods
  • Typing operators incorrectly like '=+' or '-+'
  • Confusing removal syntax with addition
3. What will be the output of the following code?
delegate void Notify();
class Program {
  static void A() => Console.WriteLine("A called");
  static void B() => Console.WriteLine("B called");
  static void Main() {
    Notify del = A;
    del += B;
    del();
  }
}
medium
A. No output
B. B called A called
C. A called
D. A called B called

Solution

  1. Step 1: Understand delegate invocation order

    Methods added to a multicast delegate are called in the order they were added.
  2. Step 2: Trace the code execution

    First, method A is assigned, then method B is added. Calling del() invokes A then B.
  3. Final Answer:

    A called B called -> Option D
  4. Quick Check:

    Multicast delegates call methods in added order [OK]
Hint: Methods run in order added to delegate [OK]
Common Mistakes:
  • Assuming reverse order of method calls
  • Expecting only the last method to run
  • Thinking no output occurs without explicit loop
4. Identify the error in this code snippet:
delegate void Notify();
class Program {
  static void A() => Console.WriteLine("A called");
  static void Main() {
    Notify del = A;
    del -= A;
    del();
  }
}
medium
A. Syntax error in delegate declaration
B. del is null when invoked, causing a runtime error
C. Cannot remove method from delegate
D. No error, output is 'A called'

Solution

  1. Step 1: Analyze delegate removal

    Removing the only method from the delegate leaves it null.
  2. Step 2: Check invocation after removal

    Calling del() when del is null causes a NullReferenceException at runtime.
  3. Final Answer:

    del is null when invoked, causing a runtime error -> Option B
  4. Quick Check:

    Removing last method makes delegate null [OK]
Hint: Removing last method makes delegate null, check before calling [OK]
Common Mistakes:
  • Assuming delegate still holds method after removal
  • Ignoring null check before invoking delegate
  • Thinking removal causes syntax error
5. You want to create a multicast delegate that calls three methods: LogStart, ProcessData, and LogEnd. Which code correctly sets up and invokes this delegate?
hard
A. Notify del; del = LogStart; del = ProcessData; del = LogEnd; del();
B. Notify del = LogStart + ProcessData + LogEnd; del();
C. Notify del = LogStart; del += ProcessData; del += LogEnd; del();
D. Notify del = LogStart; del -= ProcessData; del += LogEnd; del();

Solution

  1. Step 1: Understand delegate combination

    Use '+=' to add methods to a multicast delegate one by one.
  2. Step 2: Evaluate each option

    Notify del = LogStart; del += ProcessData; del += LogEnd; del(); correctly initializes with LogStart, then adds ProcessData and LogEnd, then calls del().
  3. Final Answer:

    Notify del = LogStart; del += ProcessData; del += LogEnd; del(); -> Option C
  4. Quick Check:

    Use '+=' repeatedly to build multicast delegate [OK]
Hint: Add methods one by one with '+=' to build multicast delegate [OK]
Common Mistakes:
  • Using '+' operator directly without delegate type
  • Overwriting delegate instead of adding methods
  • Removing methods unintentionally before calling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard