BoSsYyY
BoSsYyY

Reputation: 563

How do you know what's returned by a method when the MSDN doesn't show it?

Okay the title is very confusing I know. So let me explain what I mean by an example:

(I've got the example from the book that I'm reading, which is "Pro C# with .NET and .NET Core)

public void RegisterWithCarEngine(CarEngineHandler methodToCall)
{ 
    if (listOfHandlers == null)
        listOfHandlers = methodToCall;
    else
       listOfHandlers = Delegate.Combine(listOfHandlers, methodToCall) as CarEngineHandler;
} 

listOfHandlers is an instance of the delegate CarEngineHandler. My question is specifically about this line:

listOfHandlers = Delegate.Combine(listOfHandlers, methodToCall) as CarEngineHandler;

You see here that I'm calling the static method Combine(). And you see that I'm casting it to CarEngineHandler. Therefore this method returns an instance of CarEngineHandler treated as Delegate.

But here's what is confusing me. How do I know that it's returning an instance of CarEngineHandler when the MSDN doesn't say that?

All that the MSDN says about it is this:

Return Value
Type: System.Delegate
A new delegate with an invocation list that concatenates the invocation lists of the delegates in the delegates array. Returns null if delegates is null, if delegates contains zero elements, or if every entry in delegates is null.

Upvotes: 3

Views: 74

Answers (1)

user743382
user743382

Reputation:

You're completely right that this is something that should be documented. It's sort of implied by the fact that all delegates are required to be of the same type (as documented under "Exceptions"), but that doesn't actually promise anything. It's also required by "Combine is useful for creating event handlers that call multiple methods each time an event occurs." but that doesn't actually promise anything either.

Unfortunately, this is just one of those cases where you can tell from the fact that MS themselves rely on Delegate.Combine behaving this way that it isn't ever going to change.

Upvotes: 4

Related Questions