Digit Separators in C# 7

Digit Separators in C# 7 with examples

In this article, I am going to discuss the Digit Separators in C# 7 with Examples. Please read our previous article where we discussed Pattern Matching in C# 7 with Examples. At the end of this article, you will understand What is Digit Separator in C# and When and How to use Digit Separators with Examples.

What is Digit Separator in C#?

In reality, it’s very difficult to read a very large number. To overcome this problem, C# 7 comes with a new feature called Digit Separators i.e. “_”. Now, it is possible to use one or more Underscore (_) characters as digit separators to represent a very big number. Sometimes, it is required when we are going to represent a very big number. 

Example to Understand Digit Separator in C# 7

Let’s understand Digit Separators in C# with an example. Please have a look at the below code. As you can see, here we have created two variables. The first variable is holding a long value while the second variable is also holding a long value but that value is separated by underscores. If you look at the value, both are the same. But the readability is better in the case of the second variable which is split by underscores.

using System;
class Program
{
    static void Main()
    {
        // Both are equivalent.
        var bigNumber = 123456789012345678;
        var bigNumberSplit = 123_456_789_012_345_678;

        Console.WriteLine("bigNumber : {0}, bigNumberSplit : {1}", bigNumber, bigNumberSplit);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

When we run the application it gives you the following output.

Digit Separators in C# 7

If you observe the code, then you will feel a little difficult to read the first number as it is a very big number. But, you can easily read the second number because of the thousands separator. The separators do not make any difference in the value as you can see in the above output. You can place them wherever you like in the number, and in any quantity. 

Example to Understand Digit Separator with Double Values

It is also not mandatory to use a single underscore as a separator even though you can also use multiple separators. And it is not restricted to using them with integers only; they also work with other numeric types like double, and float as well. So, let us see an example, where we will use more than one underscores as digit separators as well as working with double value. Please have a look at the following example. Modify the main method as shown below.

using System;
class Program
{
    static void Main()
    {
        var myData1 = 1__2________3___4____5_____6;
        var myData2 = 1__2___3___4____5_____6.79;
        Console.WriteLine("bigNumber : {0}, bigNumberSplit : {1}", myData1, myData2);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Now when we run the application, it gives us the output as expected as shown in the below image.

Digit Separators in C#

In the next article, I am going to discuss the Tuples in C# 7 with Examples. Here, in this article, I try to explain Digit Separators in C# 7 with Examples. I hope you understood the use and need for Digit Separators with Examples.

Leave a Reply

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