Executes one of a series of handlers based on the dynamic type of a class instance.
switchObject's dynamic type is checked for compatibility with the argument type of each
choice in turn until a match is found. When there's a match, the choice will be called with switchObject cast to that type. If the choice returns a value, castSwitch will return it.
- If one choice's return type is void and one can return a value,
SwitchError will be thrown if the void handler was matched and completed execution without throwing. - A void handler is allowed to return when all the choices' return types convert to void - in that case,
castSwitch itself will return void. - A choice's return type can be
noreturn (e.g. when a handler always throws an exception). Throws
If none of the choices match, a
SwitchError will be thrown.
SwitchError will also be thrown if one choice can return a value but a void choice was matched and completed execution.
Parameters
choices | A sequence of function and/or delegate handlers that each accept one argument. There can also be one choice that accepts zero arguments, which will be invoked if the switchObject is null. |
switchObject | the object against which the tests are being made. |
Returns
The return value of the selected choice.