Q. You need to write a multicast delegate that accepts a DateTime argument and returns a Boolean value. Which code segment should you use?The first observation I'd make about the available answers it that they are all missing the parameter names. They may also be missing
public delegate int PowerDeviceOn(bool, DateTime);
public delegate bool PowerDeviceOn(Object, EventErgs);
public delegate void PowerDeviceOn(DateTime);
public delegate bool PowerDeviceOn(DateTime);
ref
and/or out
keywords. Who knows. I'll assume that only the parameter names are missing.
Looking past that, I've seen comments floating round the web that "Multicast delegates must contain only methods that return void". This is misleading. It's quite possible to have a return value. See the MSDN article Multicast Delegate Internals, particularly the "Method Returns and Pass-By-Reference" section towards the end.
The issue identified in the above article is that if the "subscribers return a value, it is ambiguous which subscriber’s return value would be used". In cases such as this where the return value from each invoked method is of interest the MulticastDelegate.GetInvocationList() method can be used to manually enumerate through the list of subscribers and call them individually.
While it is possible to have return types other than void I'd be wary of doing so in practice unless there was a compelling reason.