Number separator in C# _

When writing C# code, we often use very large numbers, such as the variables defined below:

const long loops = 50000000000;

Can you quickly read out how much this is? Is there still a lot of people who position the cursor to the last digit, and then press the left key on the keyboard to count up one by one: one, ten, one hundred, one thousand, ten thousand, one hundred thousand, one million, ten million, one hundred million, One billion, tens of billions, hundreds of billions...

reading large number

This way of writing numbers seriously affects its readability. That's why people invented writing methods such as thousandths. The numbers above can be written in thousandths 50,000,000,000. Isn't this more readable? But this is how Westerners write it.

If it can be written according to our Chinese habits, can 500,0000,0000it be read at a glance to be 50 billion ?

So the question is, can we write this in the code to enhance the readability of the numbers?

The answer is yes, but with a slight change-replace the comma with an underscore _:

const long loops = 500_0000_0000;
// 或者
const long loops = 50_000_000_000;

This is a function supported since C# 7.0. Its usage is similar to the usage in Java and Python.

When we use the underscore _as a numeric separator, it can be added to the digital text in addition to the first character and the last character in any location. Add the underscore to different positions of the long number to form different groups, and different groups may have different meanings in different situations. For example, we use decimal, hexadecimal or binary notation when the digital method statement, you can add an underscore _character, the numbers easier to read.

Let's take an example and define the following set of numbers:

int bin = 0b1001_1010_0001_0100;// 二进制表示
int hex1 = 0x64_95_ED;          // 十六进制表示
int hex2 = 0x_64_95_ED;         // 十六进制表示
int dec1 = 1_000_000;           // 十进制
int dec2 = 100_0000;            // 十进制
int weird = 1_2__3___4____5_____6______7_______8________9;
double real = 1_000.111_1e-5;
decimal d = 1_222_345;

The above numbers declare that the code that actually runs after compilation is:

int bin = 39444;
int hex1 = 6591981;
int hex2 = 6591981;
int dec1 = 1000000;
int dec2 = 1000000;
int weird = 123456789;
double real = 0.010001111;
decimal d = 1222345m;

It can be seen that although the results are the same, the underscore separator is appropriately added, which greatly increases the ease of reading.

Any number in C# can be separated by an underscore, and multiple underscores are allowed between two consecutive number characters. But there are times to pay attention, for example, when used in decimals and exponents, they cannot appear before and after the decimal point (10_.0), before and after the exponent character (1.1e_1), and before the type specifier (10_f), etc... …

Let's look at some wrong use cases, the following usages are all wrong:

double d1 = 1.1_e1;     //不能出现在指数字符前后
float f1 = 10_f;        //不能出现在类型说明符前面
double d2 = 10_.0;      //不能出现在小数点前后
float pi1 = 3_.1415F;   //不能出现在小数点前后
float pi2 = 3._1415F;   //不能出现在小数点前后
int x1 = 52_;           //不能出现在第一个字符和最后一个字符
int x2 = 0x52_;         //不能出现在第一个字符和最后一个字符
int x3 = 0_x52;         //不能出现在 0x 之间

to sum up

When you define a higher number, use an underscore _as a delimiter, you can make longer numbers more readable. Although this feature is trivial, when you use it, legibility will make you feel happy, right?

Underscore separator _has no effect on running on the semantics at compile time because it is ignored by the compiler.


Author: Technical Zemin
Publisher: Technical Translation Station

Public Number: Technical Translation Station

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/109750230