总结:
int有符号,显示范围为-2147483648到2147483648,即-2^31到2^31次幂。
uint没有符号,表示2^32或0到4294967295的范围。
uint可以使用十进制、二进制或十六进制。
可以与long、ulong、float、double、decimal等预定义隐式变换。 但是,需要注意值是否在可转换的范围内。 否则,会发生异常。
theuintkeywordsignifiesanintegraltypethatstorescaluesaccordingtothesizeandrangesshowninthefollowingtable。
关键字表示根据下表中列出的大小和范围存储值的整数类型。
其中范围4,294,967,295,其实是2^32-1,为什么要减1呢? 其实在计算机语言中,是从0开始的。
note : theuinttypeisnotcls-compliant.useintwheneverpossible。
注:单元类型不符合CLS。 请尽可能地使用int。
CLS表示通用语言规范通用语言规范。
标题文本
youcandeclareandinitializeauintvariablebyassigningadecimalliteral,a hexadecimal literal, or(startingwithc#7.0 ) abinaryliteraltoit.iftheintegerliteralisoutsidetherangeofuint ) thatis,ifitislessthanuint 32.mimmeteralitition
可以通过指定十进制文本、十六进制文本或以C#7.0开始的二进制文本来声明和初始化uint变量。 如果整数文本超出uint范围,即小于Uint32.MinValue或大于Uint32.MaxValue,则会发生编译错误。
In the following example,integersequalto 3,000,000,000 thatarerepresentedasdecimal,hexadecimal,andbinaryliteralsareareasssimal
在以下示例中,将一个表示为十进制、十六进制和二进制文本且等于3,000,000,000的整数赋给了uint值。
uint uintValue1=3000000000; console.writeline(uintvalue1; uint uintValue2=0xB2D05E00; console.writeline(uintvalue2; uintuintvalue3=0b 1011 _ 0010 _ 1101 _ 0000 _ 0101 _ 1110 _ 0000 _ 0000; console.writeline(uintvalue3; //theexampledisplaysthefollowingoutput ://3000000000//300000000//300000000 /。
Remark:注释:
yousetheprefix0x or0xtodenoteahexadecimalliteralandtheprefix0bo r0 btodenoteabinaryliteral.decimalliteralshavenoprefix。
可以使用前缀ox或0X表示十六进制文本,使用0b或0b表示二进制文本。 十进制文本没有前缀。
Starting with C#7.0,acoupleoffeatureshavebeenaddedtoenhancereadability。
c# 7.0 allowstheusageoftheunderscorecharacter,_,as a digit separator。
c# 7.2 allows _ tobeusedasadigitseparatorforabinaryorhexadecimalliteraliteral,aftertheprefix.adecimalliteralisn ' tpermitttedtedtohatted
C#7.0以后,为了提高可读性添加了几个功能。
在C#7.0中,可以使用下划线字符(_ )作为数字分隔符。
在C#7.2中,可以使用_作为二进制或十六进制文本的数字分隔符
之后,十进制文本不能够有前导下划线。There are some examples below:
uint uintValue1 = 3000000000;Console.WriteLine(uintValue1);uint uintValue2 = 0xB2D0_5E00;Console.WriteLine(uintValue2);uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;Console.WriteLine(uintValue3);uint uintValue2 = 0x_B2D0_5E00;Console.WriteLine(uintValue2);uint uintValue3 = 0b_1011_0010_1101_0000_0101_1110_0000_0000;Console.WriteLine(uintValue3);//The example displays the following output:// 3000000000// 3000000000// 3000000000// 3000000000// 3000000000Integer literals can also include a suffix that denotes the type. The suffic u or 'u' denotes either a uint or a ulong, depending on the numeric value of the literal. The following example uses the u suffix to denote an unsigned integer of both types. Note that the first literal is a uint because its value is less than Uint32.MaxValue, while the second is a ulong because its value is greater than Uint32.MaxValue.
整数文本还可以包含表示类型的后缀。后缀u或‘u’表示uint或ulong,具体取决于文本的数字值。下面的示例使用u后缀来表示这两种类型的无符号整数。请注意第一个文本为uint,因为其值小于Uint32.MaxValue,而第二个文本为ulong,因为其值大于Uint32.MaxValue.
object value1 = 4000000000u;Console.WriteLine($"{value1} ({4000000000y.GetType().Name})");object value2 = 6000000000u;Console.WriteLine($"{value2} ({6000000000y.GetType().Name})");If an integer literal has no suffix, its type is the first of the following types in which its value can be represented:
如果整数文本没有后缀,则其类型为以下类型中可表示其值的第一个类型。
int
uint
long
ulong
Conversions 转换
There is a predefined implicit conversion from uint to long, ulong, float, double, or decimal. For example:
存在从uint到long,ulong,float,double或decimal的预定义隐式转换。例如:
float myFloat = 4294967290;
There is a predefined implicit conversion from byte, ushort, or char to uint. Otherwise you must use a cast. For example, the following assignment statement will produce a compilation error without a cast:
存在从byte,ushort或char到uint的预定义隐式转换。否则必须使用转换。例如,如果不使用转换,一下赋值语句会生成编译错误。
long aLong = 22;//Error -- no implicit conversion from longuint uInt1 = aLong;//OK -- explicit conversion:uint uInt2 = (uint)aLong;Notice also that there is no implicit conversion from floating-point types to uint. For example, the following statement generates a compiler error unless an explicit cast is used:
另外注意,不存在从浮点类型到uint类型的隐式转换。例如,除非使用显示强制转换,否则以下语句将生成编译器错误:
//Error -- no implicit conversion from double:uint x = 3.0;//OK -- explicit conversion:uint y = (uint)3.0;