Improve code readability using Digit Separator in C# 7.0

By | August 3, 2017

During development, often we use literal in our code. They are the fixed values which may not alter during the code execution. Sometime these values are large, and sometime small.  If it is small, it’s ok. If it long representation, it could be hard to read. Making them separate could help read them better.  C# 7.0 allows ‘_’ (underscorein literals for improving the readability of your code.

To improve the readability, you can use the ‘_’ in between the number as shown in below snippet.

var number = 123_456_1617;

‘_’ used here just link a Digit Separator. Having ‘_’ has no effects on the values, and you can use the ‘number’ variable as like another variable.

Digit Separator

Here is another example of using ‘_’ with constant values.

const int number = 123_456_1617;

Related Tip: Directly throw Exception as an Expression – Throw expressions in C# 7.0

Not only with the digits, you can use the Binary Literals for representing the binary number.

Binary.PNG

This one is a very small enhancement, however it helps you make your code more readable.

Hope this helps.

One thought on “Improve code readability using Digit Separator in C# 7.0

  1. Pingback: Dew Drop - August 4, 2017 (#2535) - Morning Dew

Leave a Reply

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