C# 7 Digit Separator

Sometimes, when you want to represent a big integer or long literal in C# code, it becomes less readable and there are chances to misspell a digit too.
 
Consider the following example of a C# constant,
  1. const int assumedPopulation = 1122339654;    
You will find the one line code is neat and clean, but the numeric value/literal is not so readable.
 
It would be better if there was some digit separator like we have in Excel for numbers. Then, the number would be easy to read, like below.

1,122,339,654 
 
With C# 7, Microsoft has introduced a digit separator. While it's not the regular digit separator "comma" which we use everywhere, it's underscore.
 
In C# 7, the same line of code can be written as,
  1. const int assumedPopulation = 1_122_339_654;  
Doesn't it make the number quite easily readable?
 
And this digit separator does not affect its display on UI.
 
Write the following code in a C# 7 console app (With VS 2017), and check the output on the console. The digit separator will not be printed on console.
  1. static void Main(string[] args)  
  2. {  
  3.    const int assumedPopulation = 1_122_339_654;  
  4.    Console.WriteLine(assumedPopulation);  
  5.    Console.ReadKey();  
  6. }  
The C# 7 digit separator can be used with long, ulong, decimal, int, uint, short, ushort, double, byte, float data types.
Author
Nilesh Shah
11 54.3k 1.3m
Next » Local Functions