Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are binary literals and digit separators in C# 7.0?
C# 7.0 introduced two important enhancements to numeric literals that improve code readability and provide new ways to represent numbers: binary literals and digit separators. These features make it easier to work with binary values and large numbers in your code.
Binary Literals
Before C# 7.0, you could only assign decimal and hexadecimal values to variables. Binary literals allow you to directly assign binary values using the 0b or 0B prefix, making it easier to work with bit flags, masks, and other binary operations.
Syntax
var binaryNumber = 0b10101010; // Binary literal var binaryNumber2 = 0B11110000; // Alternative prefix
Example
using System;
class Program {
public static void Main() {
var bn = 0b1000001; // Binary for 65
Console.WriteLine("Type: " + bn.GetType());
Console.WriteLine("Value: " + bn);
Console.WriteLine("Character: " + Convert.ToChar(bn));
// More binary examples
var flags = 0b11110000; // 240 in decimal
Console.WriteLine("Flags value: " + flags);
}
}
The output of the above code is −
Type: System.Int32 Value: 65 Character: A Flags value: 240
Digit Separators
Digit separators use the underscore character (_) to improve readability of numeric literals. They can be used with any numeric type including decimal, hexadecimal, and binary literals. The separators are ignored by the compiler and serve purely for visual clarity.
Syntax
long salary = 1_000_000; // Decimal with separators int hex = 0xFF_EC_DE_5E; // Hexadecimal with separators var binary = 0b1010_0001_1000_0101; // Binary with separators
Example
using System;
class Program {
public static void Main() {
// Using digit separators with different numeric types
long salary = 1_00_00_00_00_000; // 1 trillion
Console.WriteLine("Salary type: " + salary.GetType());
Console.WriteLine("Salary value: " + salary);
// Binary with separators for better readability
var permissions = 0b1111_0000_1010_0101;
Console.WriteLine("Permissions: " + permissions);
// Hexadecimal with separators
int color = 0xFF_A5_00; // Orange color
Console.WriteLine("Color value: " + color);
}
}
The output of the above code is −
Salary type: System.Int64 Salary value: 100000000000 Permissions: 61605 Color value: 16754944
Combining Binary Literals and Digit Separators
Example
using System;
class Program {
public static void Main() {
// Binary literals with digit separators for bit manipulation
byte readWrite = 0b1100_0000; // First two bits set
byte execute = 0b0010_0000; // Third bit set
byte combined = (byte)(readWrite | execute);
Console.WriteLine("Read/Write: " + Convert.ToString(readWrite, 2).PadLeft(8, '0'));
Console.WriteLine("Execute: " + Convert.ToString(execute, 2).PadLeft(8, '0'));
Console.WriteLine("Combined: " + Convert.ToString(combined, 2).PadLeft(8, '0'));
Console.WriteLine("Combined decimal: " + combined);
}
}
The output of the above code is −
Read/Write: 11000000 Execute: 00100000 Combined: 11100000 Combined decimal: 224
Comparison
| Feature | Before C# 7.0 | C# 7.0 and Later |
|---|---|---|
| Binary values | Convert.ToInt32("1010", 2) | 0b1010 |
| Large numbers | 1000000000 (hard to read) | 1_000_000_000 |
| Hexadecimal | 0xFFECDE5E | 0xFF_EC_DE_5E |
Conclusion
Binary literals and digit separators in C# 7.0 significantly improve code readability when working with numeric values. Binary literals provide a direct way to represent binary values, while digit separators make large numbers and bit patterns more readable without affecting the actual numeric value.
