C# 7.0 – #2. Numbers code readability

These are some minor but nice improvements regarding numbers code readability:

Now it is possible to write digit separator of number literals:
The digit separator is the “_” character, like in Java language, isn’t it ?

int number = 1_345_782
Console.WriteLine(number); // prints 1345782

This is mostly useful for large numbers.
The separator is ignored by the compiler; it’s just used to improve the numbers readability, and it can be placed anywhere inside the number, but only inside, not at beginning or at the end for instance:

// this not compile
int number = _1_345_782
int number = 1_345_782_

Strange but true, the separator can be multiple, i.e. you can place more than one separator one beside another:

// this is allowed
int strangeNumber = 1_____2______3______4______5;

For decimal number, it cannot be placed right before the separator character:

// this not compile
double numberDouble = 12_.34;

Same for type specifier:

// this not compile
float numbewFloat = 12_f;

Finally it is available the literal for binary constant values (0b):

int binaryNumber = 0b0100_0010;
Console.WriteLine(binaryNumber); // prints 66

2 thoughts on “C# 7.0 – #2. Numbers code readability

Leave a Reply to mtammacco Cancel reply

Your email address will not be published. Required fields are marked *