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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.