uint和int的区别

uint和int的区别

2018年07月17日 15:44:16 我仅是只程序媛吗 阅读数 14591

总结:

int是带符号的,表示范围是:-2147483648到2147483648,即-2^31到2^31次方。

uint则是不带符号的,表示范围是:2^32即0到4294967295。

uint可以使用十进制,二进制,十六进制。

和long,ulong,float,double,decimal等预定义可以进行隐式转换。但是需要注意值是否在可转换的范围内,不然会出现异常。

The Uint keyword signifies an integral type that stores calues according to the size and ranges shown in the following table.

关键字表示一种整型类型,该类型根据下表显示的大小和范围存储值。

其中范围 4,294,967,295,其实是2^32-1,为什么要减1呢?其实是因为计算机语言中,是由0开始的。

Note:The uint type is not CLS-compliant. Use int whenever possible.

请注意:uint类型不符合CLS。请尽可能使用int。

CLS 表示的含义,就是Common Language Specification 公共语言规范。

Literals 文本

You can declare and initialize a uint variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7.0) a binary literal to it. If the integer literal is outside the range of uint (that is, if it is less than Uint32.MinCalue or greater than Uint32.MaxValue), a compilation error occurs.

你可以通过为其分配十进制文本,十六进制文本或(从C#7.0开始)二进制文本来声明和初始化uint变量。如果整数文本在uint范围之外(即,如果它小于Uint32.MinValue或大于Uint32.MaxValue),会发生编译错误。

In the following example, integers equal to 3,000,000,000 that are represented as decimal, hexadecimal, and binary literals are assigned to uint values.

在下面的实例中,表示为十进制,十六进制和二进制文本且等于3,000,000,000的整数被分配给uint值。


 
  1. uint uintValue1 = 3000000000;

  2. Console.WriteLine(uintValue1);

  3. uint uintValue2 = 0xB2D05E00;

  4. Console.WriteLine(uintValue2);

  5. uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;

  6. Console.WriteLine(uintValue3);

  7. // The example displays the following output:

  8. // 3000000000

  9. // 3000000000

  10. // 3000000000

Remark: 备注:

You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.

你可以使用前缀ox或者0X表示十六进制文本,使用0b或者0B来表示二进制文本,十进制文本是没有前缀的。

Starting with C#7.0, a couple of features have been added to enhance readability.

  • C#7.0 allows the usage of the underscore character, _, as a digit separator.

  • C#7.2 allows _ to be used as a digit separator for a binary or hexadecimal literal, after the prefix. Adecimal literal isn't permitted to have a leading underscore.

从C#7.0开始,添加了一些功能以增强可读性。

  • C#7.0允许将下划线字符(_)用作数字分隔符。

  • C#7.2允许将_用作二进制或十六进制文本的数字分隔符,位于前缀之后,十进制文本不能够有前导下划线。

There are some examples below:


 
  1. uint uintValue1 = 3000000000;

  2. Console.WriteLine(uintValue1);

  3. uint uintValue2 = 0xB2D0_5E00;

  4. Console.WriteLine(uintValue2);

  5. uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;

  6. Console.WriteLine(uintValue3);

  7. uint uintValue2 = 0x_B2D0_5E00;

  8. Console.WriteLine(uintValue2);

  9. uint uintValue3 = 0b_1011_0010_1101_0000_0101_1110_0000_0000;

  10. Console.WriteLine(uintValue3);

  11.  
  12. //The example displays the following output:

  13. // 3000000000

  14. // 3000000000

  15. // 3000000000

  16. // 3000000000

  17. // 3000000000

Integer 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.


 
  1. object value1 = 4000000000u;

  2. Console.WriteLine($"{value1} ({4000000000y.GetType().Name})");

  3. object value2 = 6000000000u;

  4. 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:

如果整数文本没有后缀,则其类型为以下类型中可表示其值的第一个类型。

  1. int

  2. uint

  3. long

  4. 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的预定义隐式转换。否则必须使用转换。例如,如果不使用转换,一下赋值语句会生成编译错误。


 
  1. long aLong = 22;

  2. //Error -- no implicit conversion from long

  3. uint uInt1 = aLong;

  4. //OK -- explicit conversion:

  5. 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类型的隐式转换。例如,除非使用显示强制转换,否则以下语句将生成编译器错误:


 
  1. //Error -- no implicit conversion from double:

  2. uint x = 3.0;

  3. //OK -- explicit conversion:

  4. uint y = (uint)3.0;

原文地址:https://www.cnblogs.com/grj001/p/12224793.html