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

Chuyển đổi ngầm từ ushort (16-bit unsigned) sang Decimal trong C#

ushort (16-bit unsigned integer, 0 đến 65,535) có thể chuyển đổi ngầm (implicit conversion) sang decimal mà không mất dữ liệu.

Ví dụ minh họa

using System;

public class Demo
{
    public static void Main()
    {
        ushort val = 2567;
        decimal dec;

        Console.WriteLine("Chuyển đổi ngầm từ 16-bit unsigned integer sang Decimal");
        dec = val;
        Console.WriteLine("Decimal = " + dec);
    }
}

Kết quả đầu ra

Chuyển đổi ngầm từ 16-bit unsigned integer sang Decimal
Decimal = 2567

Quy tắc chuyển đổi ngầm số: byte → ushort → uint → ulong → float → double → decimal. Ngược lại cần ép kiểu tường minh (cast).