C# Delegates, Events, and Anonymous Methods Essentials Quiz Quiz

Challenge your understanding of C# delegates, events, and anonymous methods with this beginner-friendly quiz. Perfect for learners aiming to reinforce key concepts, syntax rules, and practical usage of these features in real-world C# programming.

  1. Delegate Definition

    Which statement correctly describes a delegate in C#?

    1. A delegate is a data structure for storing multiple classes.
    2. A delegate is a variable that holds only numeric values.
    3. A delegate is a type that represents references to methods with a specific parameter list and return type.
    4. A delegate is the same as a constant variable.

    Explanation: A delegate allows methods to be passed as parameters and can reference any method with a matching signature. Delegates are not used for storing numeric values, which is what variables do. Likewise, delegates are not a data structure for classes or a constant. Only the first option correctly states the purpose of delegates.

  2. Delegate Declaration

    Which of the following shows a correct way to declare a delegate that returns void and accepts one int parameter?

    1. void delegate MyDelegate(int);
    2. public delegate void MyDelegate(int value);
    3. public delegate void MyDelegate();
    4. delegate int MyDelegate(int value);

    Explanation: The correct syntax for declaring a delegate that takes an int and returns void is 'public delegate void MyDelegate(int value);'. The second option misplaces the 'void' keyword. The third option declares a delegate returning int, not void. The last option misses the int parameter.

  3. Delegate Instantiation

    How do you correctly create an instance of a delegate named MyDelegate pointing to a method PrintNumber?

    1. MyDelegate del = PrintNumber();
    2. MyDelegate del = new MyDelegate(PrintNumber);
    3. PrintNumber del = MyDelegate();
    4. MyDelegate del = new PrintNumber(MyDelegate);

    Explanation: Creating a delegate instance requires the 'new' keyword followed by the delegate type and the method name. The second option attempts to call the method instead of assign it. The third and fourth options mix up the syntax and types.

  4. Invoking a Delegate

    Given 'MyDelegate del = new MyDelegate(PrintNumber);', which is the correct way to invoke the delegate with parameter 5?

    1. del-u003EPrintNumber(5);
    2. del.invoke(5);
    3. del(5);
    4. MyDelegate(5);

    Explanation: Delegate instances are invoked just like a method, so 'del(5);' is correct. 'del.invoke(5);' is not valid syntax—Invoke is used as 'del.Invoke(5);' (with capitals). The arrow operator and directly invoking the type are both incorrect.

  5. Multicast Delegates

    What is the main feature of a multicast delegate in C#?

    1. It changes integer values automatically.
    2. It can only reference methods with no parameters.
    3. It stores events in memory.
    4. It can hold references to multiple methods.

    Explanation: Multicast delegates let you assign several methods to a single delegate instance, so that all are called in sequence. They do not change integers automatically, do not store events directly, and are not limited to parameterless method signatures.

  6. Anonymous Method Syntax

    Which C# syntax creates an anonymous method that returns the square of a number?

    1. public delegate int x = x * x;
    2. new delegate int x = x * x;
    3. delegate(int x) { return x * x; }
    4. delegate x =u003E x * x;

    Explanation: The correct syntax for an anonymous method is 'delegate(parameterlist) { body }'. The second option misuses 'new' and the delegate declaration. The third confuses anonymous methods with lambda expressions, and the fourth is an incorrect delegate declaration.

  7. Event Keyword Usage

    What does the 'event' keyword do when declaring a delegate field in a class?

    1. Allows direct access and invocation by any class.
    2. Restricts external classes to only add or remove handlers, not invoke the delegate directly.
    3. Marks the delegate as a static-only field.
    4. Automatically executes the delegate when a variable changes.

    Explanation: When a delegate is declared with the 'event' keyword, outside code can only subscribe or unsubscribe, not invoke the event. The 'event' keyword does not trigger execution or require static use, nor does it allow unlimited access.

  8. Event Subscription

    How does a method subscribe to a C# event named 'OnChange'?

    1. HandlerMethod += OnChange;
    2. OnChange += HandlerMethod;
    3. OnChange = HandlerMethod;
    4. Event.Subscribe(OnChange, HandlerMethod);

    Explanation: To subscribe to an event, you use the '+=' operator with the event and handler method. '=' would replace all existing handlers. There is no built-in 'Event.Subscribe', and the last option reverses the order.

  9. Lambda vs Anonymous Methods

    Which statement best distinguishes a C# lambda expression from an anonymous method?

    1. Anonymous methods are always faster than lambdas.
    2. There is no functional difference; they have identical syntax.
    3. A lambda expression uses the '=u003E' syntax, while an anonymous method uses the 'delegate' keyword.
    4. Anonymous methods allow expression-bodied syntax; lambdas do not.

    Explanation: Lambdas use the '=u003E' syntax and can often be more concise, while anonymous methods use 'delegate'. There is not always a speed difference, and both can use expression or statement bodies. Their syntax is not identical.

  10. Event Handler Signature

    Which is the recommended signature for an event handler in C#?

    1. int Handler(int sender, int e)
    2. void Handler()
    3. bool Handler(object source, string[] args)
    4. void Handler(object sender, EventArgs e)

    Explanation: Event handlers in C# conventionally use 'void' return, the sender object, and an event argument. The second and fourth have wrong parameter types, and the third lacks parameters.

  11. Delegate Type Safety

    Why are delegates considered type-safe in C#?

    1. Delegates store any kind of variable or data type.
    2. Delegates ignore return types while storing methods.
    3. Delegates can reference any method regardless of its signature.
    4. Delegates can only reference methods matching their signature.

    Explanation: Delegates enforce at compile-time that only methods with the same return type and parameters can be assigned. They cannot reference just any method, nor do they store arbitrary data. Return type mismatches are not allowed.

  12. Chaining Delegates

    Which operator is used to combine multiple delegates into a multicast delegate?

    1. -
    2. u0026
    3. +
    4. *

    Explanation: The '+' operator is used to chain delegates so that multiple methods can be called. '-' is used to remove, '*' and 'u0026' are not valid for this operation.

  13. Removing Event Subscriptions

    How can you unsubscribe a method named 'HandleEvent' from an event called 'OnNotify'?

    1. OnNotify.delete(HandleEvent);
    2. remove OnNotify HandleEvent;
    3. OnNotify -= HandleEvent;
    4. OnNotify.Unsubscribe(HandleEvent);

    Explanation: '-=' removes a method from an event's invocation list. The other options do not represent valid C# syntax for unsubscribing from events.

  14. Passing Delegates as Parameters

    Which C# concept allows passing a delegate as a parameter to a method for callbacks?

    1. Indexer
    2. Constructor chaining
    3. Static variable
    4. Callback pattern

    Explanation: Passing a delegate allows a method to 'call back' the passed function, which is known as the callback pattern. Constructor chaining relates to constructors, indexers are for array-like access, and static variables are for shared data.

  15. Access Level of Events

    What is the default access modifier of an event declared within a class?

    1. Protected
    2. Internal
    3. Private
    4. Public

    Explanation: If no access modifier is specified, an event is private by default within a class. Public is the default only for members of structs, and internal or protected require explicit declaration.

  16. Delegate Return Values

    What value is returned if a multicast delegate calls several methods that return int?

    1. A list containing all return values.
    2. The sum of all returned values.
    3. The value from the first method only.
    4. Only the value from the last method in the invocation list.

    Explanation: A multicast delegate with non-void return type returns only the result from the last method called. It does not sum, collect, or return values from all methods. Only the last value is accessible.