O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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” ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required