An Operator’s Result Type

What about the type produced by the operator? Because either the second or third operand can deliver the operator’s result, there needs to be some kind of compatibility between them. When both have the exact same type, there’s no question at all. But what if they don’t line up? Then the answer lies in implicit conversions. For example:

condition ? 42 : 123L

The resulting type of this conditional operator use will be long because it’s possible to convert implicitly from int to long (without loss of data, that is). Obviously, the rule holds symmetrically if the second and third arguments are swapped:

!condition ? 123L : 42

However, in the face of type hierarchies, the operator does not look for the “least common supertype” ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.