What do the underscores mean in a numeric literal in C#?

This is a new feature of C# 7.0 and it is known as digit separator. The intent is to provide better and easier readability. It is mostly useful when writing numbers that are very long and hard to read in source code. For example:

long hardToRead = 9000000000000000000; 

// With underscores
long easyToRead = 90000_00000_00000_0000;  

It is totally up to the programmer on where to place the underscore. For example, you may have a weird scenario like this:

var weird = 1_00_0_0_000_0000000_0000;  
public const decimal GoldenRatio = 1.618_033_988_749_894_848_204_586_834_365_638_117_720M;

Some Notes

As soon as you compile your code, the compiler removes the underscores so this is just for code readability. So the output of this:

public static void Main()
{
    long easyToRead = 90000_00000_00000_0000;
    Console.WriteLine(easyToRead);
}

will be (notice no underscores):

9000000000000000000

Demo in DotNet Fiddle

Here is a discussion about this feature when it was requested if you are interested. Some people wanted the separator to be blank spaces, but looks like the C# team went with underscores.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)