C# Numeric Promotion for Conditional Expression


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.

Updated on: 21-Jun-2020

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements