C# 7.0 introduced two literal improvements, a digit separator and a hex literal.
Digit Separator
The digit separator “_” can be used inside number literals now. The purpose of a digit separator is to improve readability, nothing more. The value of the variable is unaffected by this literal.
The following code snippet shows how to use the digit separator.
- var num = 1_000_000;
- var hex = 0xAB_CD_EF;
The value of the above variables are 1000000 and ABCDEF.
Binary Literal
C# supports hexadecimal literals by using 0x. As Vulpes wrote in his article, Simulating binary literals in C#, the C# versions prior to C# 7.0 did not support binary literals.
Now, in C# 7.0, you can define binary literals using 0b. The following code snippet uses 0b and _ literals.
- var bin = 0b1000_0100_0101_0110_0111_1011;
Summary
In this article, we learned about the literal improvements introduced in C# 7.0.
References
References used to write this article,
https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
Sourav Kumar DasPosted Nov 16, 2019, 3:39 AM
Nice Article Sir.
Saurabh RaiPosted Jan 5, 2017, 10:49 AM
Wow!! This was something new and nice to know :)
Aakash MauryaPosted Dec 27, 2016, 12:16 PM
Digit separator is something which we actually needed in increasing the readability of codes where large number involved. But we never thought that this will possible in future.
Anil Kumar MurmuPosted Dec 26, 2016, 1:41 AM
Digit Separator & Binary Literal are new to me..
Ammar ShaukatPosted Dec 26, 2016, 12:55 AM
Wao.. C# team is on the way to improve its UX all the time..