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.
Which statement correctly describes a delegate in C#?
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.
Which of the following shows a correct way to declare a delegate that returns void and accepts one int parameter?
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.
How do you correctly create an instance of a delegate named MyDelegate pointing to a method PrintNumber?
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.
Given 'MyDelegate del = new MyDelegate(PrintNumber);', which is the correct way to invoke the delegate with parameter 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.
What is the main feature of a multicast delegate in C#?
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.
Which C# syntax creates an anonymous method that returns the square of a number?
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.
What does the 'event' keyword do when declaring a delegate field in a class?
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.
How does a method subscribe to a C# event named 'OnChange'?
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.
Which statement best distinguishes a C# lambda expression from an anonymous method?
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.
Which is the recommended signature for an event handler in C#?
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.
Why are delegates considered type-safe in C#?
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.
Which operator is used to combine multiple delegates into a multicast delegate?
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.
How can you unsubscribe a method named 'HandleEvent' from an event called 'OnNotify'?
Explanation: '-=' removes a method from an event's invocation list. The other options do not represent valid C# syntax for unsubscribing from events.
Which C# concept allows passing a delegate as a parameter to a method for callbacks?
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.
What is the default access modifier of an event declared within a class?
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.
What value is returned if a multicast delegate calls several methods that return int?
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.