Improve code readability using Digit Separator  in C# 7.0

Improve code readability using Digit Separator in C# 7.0

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.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

One Comment to “Improve code readability using Digit Separator in C# 7.0”

Comments are closed.