int和System的区别是什么?Int16、系统。Int32和System。Int64,而不是它们的大小?
当前回答
Int和int32是同一个(32位整数) Int16是短int(2字节或16位) Int64是长数据类型(8字节或64位)
其他回答
关于16、32和64类型有一个非常重要的注意事项:
如果你运行这个查询… 数组中。IndexOf(new Int16[]{1,2,3}, 1)
你应该得到0(0),因为你问…是1在1 2或3的数组中。 如果你得到的答案是-1,这意味着1不在1,2,3的数组中。
来看看我的发现: 下面所有的结果都应该是0而不是-1 (我已经在所有的框架版本2.0,3.0,3.5,4.0中进行了测试)
C#:
Array.IndexOf(new Int16[]{1,2,3}, 1) = -1 (not correct)
Array.IndexOf(new Int32[]{1,2,3}, 1) = 0 (correct)
Array.IndexOf(new Int64[]{1,2,3}, 1) = 0 (correct)
VB。NET:
Array.IndexOf(new Int16(){1,2,3}, 1) = -1 (not correct)
Array.IndexOf(new Int32(){1,2,3}, 1) = 0 (correct)
Array.IndexOf(new Int64(){1,2,3}, 1) = -1 (not correct)
我的观点是,对于数组。IndexOf比较,只信任Int32!
他们确实是同义的,但是我发现他们之间的小区别,
1)创建enum时不能使用Int32
enum Test : Int32
{ XXX = 1 // gives you compilation error
}
enum Test : int
{ XXX = 1 // Works fine
}
2) Int32属于System声明。如果你不使用。在系统中,你会得到编译错误,但在int中不会
编辑:这对c#来说并不完全正确,我在回答这个问题时漏掉了一个标签——如果有更具体的c#答案,请投票给它!
它们都代表不同大小的整数。
然而,有一个非常非常微小的区别。
Int16、int32和int64的大小都是固定的。
int型的大小取决于你所编译的体系结构——C规范只将int型定义为大于或等于short型,但实际上它是你的目标处理器的宽度,可能是32位,但你应该知道它可能不是。
int
它是c#中定义的基本数据类型。
它被映射到FCL类型的Int32。
它是一个值类型,表示System。Int32结构。
它是有符号的,长度为32位。
它具有最小值-2147483648和最大值+2147483647。
Int16
这是整柜型。
在c#中,short被映射为Int16。
它是一个值类型,表示System。Int16结构。
它带符号,长度为16位。
最小值为-32768,最大值为+32767。
Int32
这是整柜型。
在c#中,int被映射到Int32。
它是一个值类型,表示System。Int32结构。
它是有符号的,长度为32位。
它具有最小值-2147483648和最大值+2147483647。
Int64
这是整柜型。
在c#中,long被映射到Int64。
它是一个值类型,表示System。Int64结构。
它是有符号的,长度为64位。
它的最小值为-9,223,372,036,854,775,808,最大值为9,223,372,036,854,775,807。
每种类型的整数具有不同的存储容量范围
Type Capacity
Int16 -- (-32,768 to +32,767)
Int32 -- (-2,147,483,648 to +2,147,483,647)
Int64 -- (-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807)
正如James Sutherland在他的回答中所述:
int and Int32 are indeed synonymous; int will be a little more familiar looking, Int32 makes the 32-bitness more explicit to those reading your code. I would be inclined to use int where I just need 'an integer', Int32 where the size is important (cryptographic code, structures) so future maintainers will know it's safe to enlarge an int if appropriate, but should take care changing Int32 variables in the same way. The resulting code will be identical: the difference is purely one of readability or code appearance.
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 没有ListBox。SelectionMode="None",是否有其他方法禁用列表框中的选择?
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何在iis7应用程序池中设置。net Framework 4.5版本