C# 7.0 gave us digit separators C# 7.0 but did not permit them directly after the 0x or 0b prefix of a hexadecimal or binary literal.
C# 7.2 permits the _ character to also be used between the prefix and the first digit.
The point is alignment rather than new capability. A 0x or 0b prefix is two characters wide, so without a leading separator the byte groups in a hex literal sit one column to the left of where they would in a decimal literal. Adding 0x_ makes the groups line up:
csharp
const int utf16bigEndian = 0x_FE_FF; // groups start here
const int utf16littleEndian = 0x_FF_FE;
const int masked = 0x00_FF_00; // without, for comparisonCode
csharp
const int utf16bigEndian = 0x_FE_FF;
const int utf16littleEndian = 0x_FF_FE;
const byte flags = 0b_1010_0101;csharp
const int utf16bigEndian = 0xFE_FF;
const int utf16littleEndian = 0xFF_FE;
const byte flags = 0b1010_0101;Notes
- Only the position directly before the first digit is affected; separators between digits already worked in C# 7.0
- Decimal literals still cannot start with
_, because_123would be parsed as an identifier - A trailing separator such as
0b1010_is a compile error (CS1013) - Consecutive separators such as
1__000are allowed, though there is rarely a reason to use them - Works on binary, hexadecimal and decimal literals and on floating point ones, but only binary and hexadecimal literals have a prefix to separate
- See binary literals C# 7.0 for the
0bprefix this is most often used with