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

Binary Literals và Digit Separators trong C# 7.0

Binary Literals

Trước C# 7 chỉ có thể assign decimal và hexadecimal values cho variable.

C# 7.0 giới thiệu binary literal cho phép gán binary value cho variable.

Digit Separator

Digit Separator có dạng single underscore (_). Separator này dùng trong bất kỳ numeric literal nào để cải thiện readability.

Ví dụ Binary Literals

class Program { 
    public static void Main() { 
        var bn = 0b1000001; 
        System.Console.WriteLine(bn.GetType()); 
        System.Console.WriteLine(Convert.ToChar(bn)); 
        Console.ReadLine(); 
    } 
}

Output

System.Int32
A

Ví dụ Digit Separator

class Program { 
    public static void Main() { 
        long Salary = 1_00_00_00_00_000; 
        System.Console.WriteLine(Salary.GetType()); 
        System.Console.WriteLine(Salary); 
        Console.ReadLine(); 
    } 
}

Output

System.Int64
100000000000

Binary Literals Chi Tiết

// Binary literal prefix: 0b hoặc 0B
var b1 = 0b1010;        // 10 decimal
var b2 = 0B1111_0000;   // 240 decimal (với digit separator)
var b3 = 0b0000_1111;   // 15 decimal

// Kết hợp với các kiểu số
byte b = 0b1111_1111;   // 255
short s = 0b0111_1111_1111_1111; // 32767
int i = 0b1111_1111_1111_1111_1111_1111_1111_1111; // -1 (signed)
uint u = 0b1111_1111_1111_1111_1111_1111_1111_1111; // 4294967295
long l = 0b1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; // 2^62

Digit Separators Chi Tiết

// Decimal
int million = 1_000_000;
double pi = 3.141_592_653_589_793;

// Hexadecimal
int color = 0xFF_00_00;        // Red: FF, Green: 00, Blue: 00
long address = 0x00_1A_2B_3C_4D_5E_6F;

// Binary
byte flags = 0b1_0101_0101;    // Group by bit fields

// Floating point
double avogadro = 6.022_140_76e23;
float planck = 6.626_070_15e-34f;

Quy tắc Digit Separator

  • Chỉ dùng underscore _
  • Không đặt ở đầu hoặc cuối literal: _123 ❌, 123_ ❌
  • Không đặt liên tiếp: 1__23 ❌
  • Không đặt cạnh decimal point: 1_2.34 ❌, 12_.34 ❌
  • Không đặt cạnh exponent: 1e_10 ❌, 1e1_0 ❌
  • Không đặt cạnh prefix: 0x_FF ❌, 0b_1010 ❌

Use Cases Thực Tế

1. Bit Flags / Bitmasks

[Flags]
public enum FilePermissions : byte {
    None = 0b_0000_0000,
    Read = 0b_0000_0001,
    Write = 0b_0000_0010,
    Execute = 0b_0000_0100,
    Delete = 0b_0000_1000,
    Admin = 0b_0001_0000,
    All = Read | Write | Execute | Delete | Admin // 0b_0001_1111
}

2. Color Values (ARGB)

uint transparentRed = 0x80_FF_00_00; // Alpha=80, Red=FF, Green=00, Blue=00
uint opaqueBlue = 0xFF_00_00_FF;

3. Network/Memory Addresses

long ipv6Address = 0x2001_0DB8_0000_0000_0000_0000_0000_0001;
ulong memoryAddress = 0x0000_7FFF_FFFF_F000;

4. Large Numbers Readable

const long WorldPopulation = 7_900_000_000;
const double LightSpeed = 299_792_458; // m/s
const decimal Avogadro = 6.022_140_76e23m;

C# 7.2+ : Digit Separator sau prefix

// C# 7.2 cho phép separator ngay sau prefix
var hex = 0x_FF_00_FF;    // Valid từ C# 7.2
var bin = 0b_1010_1100;   // Valid từ C# 7.2