Which one of the following statements is true about events and delegates?

  1. Events must know what object handles its event.

  2. An event can only have one event handler.

  3. Delegates are not type-safe.

  4. A class with events can only raise a single event.

  5. A delegate can only hold methods that match the delegate's method signature


Correct Option: E

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Events must know what object handles its event - This option is incorrect. Events do not need to know which object handles them. Events are a way for objects to notify other objects when something of interest happens.

Option B) An event can only have one event handler - This option is incorrect. An event can have multiple event handlers. When an event is raised, all registered event handlers will be called.

Option C) Delegates are not type-safe - This option is incorrect. Delegates are type-safe. They provide a way to define a signature for a method and can only hold methods that match that signature.

Option D) A class with events can only raise a single event - This option is incorrect. A class with events can raise multiple events. Events provide a way for a class to notify other objects about different types of occurrences.

Option E) A delegate can only hold methods that match the delegate's method signature - This option is correct. A delegate is a type-safe function pointer that can hold references to methods with a matching signature. It ensures that the method being assigned to the delegate has the correct number and types of parameters and returns the correct type.

The correct answer is E. This option is correct because a delegate can only hold methods that match the delegate's method signature.

Find more quizzes: