Ternary operator behaviour inconsistency [duplicate]

C# language specification, version 5, section 6.1.9:

An implicit constant expression conversion permits the following conversions:

  • A constant-expression (§7.19) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.

Your first example is a constant expression, because it can be evaluated at compile time. But see section 7.19 for more details:

Only the following constructs are permitted in constant expressions:

  • Literals (including the null literal).

[…]

  • The predefined +, –, *, /, %, <<, >>, &, |, ^, &&, ||, ==, !=, <, >, <=, and >= binary operators, provided each operand is of a type listed above.
  • The ?: conditional operator.

Leave a Comment