Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Binary Literals และ Digit Separators ใน C# 7.0

Binary Literals

ก่อน C# 7 เรา Assign ได้แค่ Decimal และ Hexadecimal Values ให้ Variable ใน C# 7.0 Binary Literal ถูก Introduce อนุญาตให้ Assign Binary Value ให้ Variable โดยตรง

Digit Separator

Digit Separator ใช้ Single Underscore (_) ใช้ภายใน Numeric Literal ใดๆ เพื่อเพิ่ม Readability (อ่านง่ายขึ้น)

ตัวอย่าง Binary Literals


class Program {
    public static void Main() {
        // Binary Literal: 0b หรือ 0B นำหน้า
        var bn = 0b1000001;  // Binary สำหรับ 'A' (65)
        
        Console.WriteLine(bn.GetType());        // System.Int32
        Console.WriteLine(Convert.ToChar(bn));  // A
        Console.ReadLine();
    }
}

Output


System.Int32
A

ตัวอย่าง Binary Literals เพิ่มเติม


// Binary Literals กับ Digit Separators
int flags = 0b_0001_0010_0100_1000;  // Readable Bit Flags
int mask = 0b_1111_0000;              // High nibble mask
long bigBinary = 0b_1010_1010_1010_1010_1010_1010_1010_1010;

// Bitwise Operations กับ Binary Literals
int a = 0b_1100;  // 12
int b = 0b_1010;  // 10

Console.WriteLine($"a = {a}");           // 12
Console.WriteLine($"b = {b}");           // 10
Console.WriteLine($"a | b = {a | b}");   // 14 (1110)
Console.WriteLine($"a & b = {a & b}");   // 8  (1000)
Console.WriteLine($"a ^ b = {a ^ b}");   // 6  (0110)
Console.WriteLine($"~a = {~a}");         // -13 (Two's Complement)

ตัวอย่าง Digit Separators


class Program {
    public static void Main() {
        // Digit Separator: Underscore (_)
        long salary = 1_00_00_00_00_000;  // 1 Trillion (Thai Style Grouping)
        long salary2 = 1_000_000_000_000; // 1 Trillion (International Grouping)
        
        Console.WriteLine(salary.GetType());  // System.Int64
        Console.WriteLine(salary);            // 1000000000000
        Console.WriteLine(salary2);           // 1000000000000
        
        // ทุก Numeric Types
        double pi = 3.141_592_653_589_793;
        float avogadro = 6.022_140_76e23f;
        decimal precise = 123_456_789.123_456_789m;
        
        Console.ReadLine();
    }
}

Output


System.Int64
1000000000000
1000000000000

กฎของ Digit Separators

  • วาง _ ระหว่าง Digits ได้ทุกที่ (ไม่ใช่ต้นท้าย)
  • ไม่วางติด Decimal Point: 1_23.45 ✓, 1_.23 ✗, 1._23 ✗
  • ไม่วางติด Exponent: 1e_10 ✗, 1_0e10 ✓
  • ไม่วางติด Type Suffix: 100_f ✗, 100f ✓
  • Binary/Hex: 0b_1010 ✓, 0x_FF ✓
  • Multiple Underscores: 1__000 ✓ (แต่ไม่แนะนำ)

การรวม Binary Literals + Digit Separators


// Bit Flags ที่อ่านง่าย
[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,
    
    ReadWrite = Read | Write,           // 0b_0000_0011
    All = Read | Write | Execute | Delete // 0b_0000_1111
}

// RGB Color
uint color = 0xFF_80_00_FF;  // ARGB: Alpha=FF, Red=80, Green=00, Blue=FF

// Network Port
ushort port = 0b_0001_1111_0100_0000; // 8000

สรุป

FeatureSyntaxExampleSince
Binary Literal0b... / 0B...0b1010 (=10)C# 7.0
Digit Separator_ (underscore)1_000_000C# 7.0
Combined0b_...0b_1111_0000C# 7.0