数字分隔符
C#7提供了数字分隔符,有助于提高可读性,且不添加任何额外功能。
例如,可以在数字中间添加下划线,如下所示:
long myValue = 0x123_456_89ab_cdef;
用作分割符的下划线会被编译器忽略,实际和下面的结果是一模一样,只是提高了可读性:
long myValue = 0x12345689abcdef;
二进制值
除了提供数字分隔符,C# 7还可更容易地进行二进制赋值。如果你在一个数字前面敲了0b,接下来就只允许使用0和1,只有二进制数值允许进行赋值。示例:
uint myBinary = 0b1100_1001_1001_0011;
如果值不是二进制编译就会报错,比如:
uint i = 0b1100_1234; Console.WriteLine(i);
编译时,便会报错:
error CS1002: ; expected