Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#

Numeric literal syntax improvement in C#

Hammad Nasir

Overview

Interpreting numeric literals while reading can be cumbersome if the number is quite long (such as in the case of binary literals).

So, C# 7.0 introduces the concept of the digit separator, which improves readability.

Examples

Normally, a binary number is written as follows:

const int Eight = 0b00001000;

0b denotes that the following digits represent a binary literal. Note that it takes a little focus to separate the nibble4 digits of binary in order to interpret the number visually. However, this problem is solved by using the digit separator. The above code can be written as follows:

const int Eight = 0b0000_1000;

The _ present in the binary literal is called the digit separator. Now, it is fairly easy to interpret the binary string.

The digit separator can work on all numeric types. The following is an example:

const int Million = 1_000_000;
const int e = 2.718_281_828;
const int Pi = 3.141_592_654;

RELATED TAGS

c#

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring