In this short tutorial, you will learn C# digit separtor.
C# 7 comes with a new feature called digit separators “_”, it is able to group digits in large numeric literals would have great readability impact and no significant downside.
The C# 7 digit separator can be used with long, ulong, decimal, int, uint, short, ushort, double, byte, float data types.
using System;
int v1 = 123_000_001;
long v2 = 456_123_001;
decimal v3 = 123_000.345M;
double v4 = 456_000.345;
Console.WriteLine($"v1: {v1}; v2: {v2}; v3: {v3}; v4:{v4}");
int bin = 0b1001_1010_0001_0100;
int hex = 0x1b_a0_44_fe;
int dec = 33_554_432;
Console.WriteLine($"bin: {bin}; hex: {hex}; dec: {dec};");
Output:
v1: 123000001; v2: 456123001; v3: 123000.345; v4:456000.345
bin: 39444; hex: 463488254; dec: 33554432;
The syntax is straightforward, and the separators have no semantic impact – they are simply ignored.