Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Numeric Promotion cho Conditional Expression trong C#


Numeric promotion: chuyển kiểu nhỏ sang kiểu lớn (short -> int).

Ví dụ conditional expression tự động promote short thành int:

Ví dụ

using System;
class Program {
    static void Main() {
        short val1 = 99;
        int val2;
        val2 = (val1 == 1) ? 100 : 30;
        Console.WriteLine(val2);
    }
}

val2 là int, val1 là short. Biểu thức điều kiện promote sang int.