Using Literals In C#

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.

  1. var num = 1_000_000;  
  2. 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.

  1. var bin = 0b1000_0100_0101_0110_0111_1011;   

Summary

In this article, we learned about the literal improvements introduced in C# 7.0.

Next C# 7.0 Feature >> Pattern Matching 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/


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.