C# Numeric Promotion for Conditional Expression

CsharpProgrammingServer Side Programming

Numeric promotion is the promotion of smaller types to larger types like short to int.

In the below example, we have seen a numeric promotion in Conditional Expression.

The short types are automatically promoted to larger type int.

Example

using System;

class Program {
   static void Main() {
      short val1 = 99;
      int val2;

      val2 = (val1 == 1) ? 100 : 30;

      Console.WriteLine(val2);
   }
}

Output

Above, we have used a conditional expression that automatically promoted to int −

val2 = (val1 == 1) ? 100 : 30;

Here, val2 is an int and val is a short.

raja
Published on 14-Aug-2018 08:24:17
Advertisements