总结:

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值。

uint uintValue1 = 3000000000;
Console.WriteLine(uintValue1);
uint uintValue2 = 0xB2D05E00;
Console.WriteLine(uintValue2);
uint uintValue3 = 0b1011_0010_1101_0000_0101_1110_0000_0000;
Console.WriteLine(uintValue3);
// The example displays the following output:
//  3000000000
//  3000000000
//  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:

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

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:

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

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

long aLong = 22;
//Error -- no implicit conversion from long
uint 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;

uint和int的区别相关推荐

  1. uint与int的区别

    为什么与如何使用uint 开始时这里有几个无符号的整数,这没有什么问题,如果你深入研究机器码去看你会发现在任何设备上它们都不过是无符号整数.所有其他的值比如说int,float,bool,charac ...

  2. Go语言中的uint和int的区别

    话不多说,先上图 简单说明一下 , 类型大小为1,说明字节是1 , 一个字节占八位 int8的范围就是-128~127 有符号 uint的范围就是0~255 无符号

  3. int long java_java long int的区别

    java long int的区别 java中long和int都属于整型,为什么还要细分为long和int两种数据类型呢?这是因为它们代表的大小不一样.具体区别如下: 1.区别1 16位系统:long是 ...

  4. c语言uint赋值给int,如何在C#中将uint转换为int?

    假设您只想从一种类型中提取32位并将其原样转储到另一种类型中: uint asUint = unchecked((uint)myInt); int asInt = unchecked((int)myU ...

  5. 你知道Integer和int的区别吗

    最近小康面试,突然被面试官问道,说一下Integer和int的区别.额-可能平时就知道写一些业务代码以及看一些自己觉得比较高大上的东西,包括面试也看的一些Spring源码等,对于这种java特别基础的 ...

  6. NSNumber 以及NSInteger,NSNumber以及Int的区别

    //   NSNumber 之所以可以(只能)包装基本数据类型,是因为继承了NSValue; //  NSNumber 把基本数据类型包装成一个对象类型(因为集合不能存放基本数类型) //初始化 NS ...

  7. const int 和INT const区别

    const int 和INT const区别 2010-04-09 23:26 const int a = 5; int const b = 6; 没区别 指针的时候有区别,引用也有区别 指针的话 1 ...

  8. c++将int转换成string_Integer与int的区别 (== 与 equal)

    Integer与int的区别 (== 与 equal) 先来看下Java中的8种基本类型和3种引用数据类型 8种基本数据类型:boolean byte int char long short floa ...

  9. integer 负数字符串比较_Integer与int的区别 (== 与 equal)

    Integer与int的区别 (== 与 equal) 先来看下Java中的8种基本类型和3种引用数据类型 8种基本数据类型:boolean byte int char long short floa ...

  10. python中str和int区别_python中eval与int的区别浅析

    python中eval和int的区别是什么?下面给大家介绍一下: 1.eval()函数 eval(<字符串>)能够以Python表达式的方式解析并执行字符串,并将返回结果输出.eval() ...

最新文章

  1. 【Android 安装包优化】使用 lib7zr.so 动态库处理压缩文件 ( 拷贝 lib7zr.so 动态库头文件到 Android 工程中 | 配置 CMakeLists.txt 构建脚本 )
  2. 主板怎么开启csm_华擎Z490主板移植AMD SAM加速技术:游戏性能提升最多11.5%
  3. Java设计模式GOF之6大设计原则
  4. Vue + Element UI + Moment.js——el-table-column的时间戳格式转换解决方案
  5. 接口幂等性问题解决方案
  6. [BZOJ]3436: 小K的农场
  7. 中科大「少年班」对手来了!清华「丘成桐领军计划」招收优秀中学生
  8. oracle12c时间,Oracle 12c-选择冒号后的日期时间字符串
  9. 55.函数模板指针匹配(模板自动匹配*多的)
  10. 考上985能改变命运吗_2021艺考生:文化课成绩一般,有机会考上985、211吗?
  11. Fedora下SAMBA的相关配置
  12. Sort代码详解学习
  13. VASP笔记之:计算德拜温度,杨氏模量,弹性矩阵
  14. Excel 各种密码的破解,大全建议收藏!
  15. 记一次笔记本win键失灵 不能用 windows 徽标键失灵
  16. HCNP——RIPv1和RIPv2概况
  17. 多元统计:相关概念总结
  18. 将一个文件夹中的图片写成视频
  19. ARM服务器编译安装ClickHouse
  20. UpdateData() 函数

热门文章

  1. MATLAB图像处理实验——细胞图像的分割和计数
  2. 合并照片到word中
  3. latex加下划线_Latex学习系列之粗体、斜体和下划线
  4. 基于Nginx的图片预览或下载
  5. 十行Python代码替换证件照背景颜色
  6. c语言switch excepted,C语言问题 expected unqualified-id
  7. gitlab 注册runner
  8. 守望先锋外挂(OWG)透视原理分析
  9. word设置表格文字紧贴下框线
  10. wps删除第二页页眉