在c#中,int和Int32是同一个东西,但我读过很多次int比Int32更受欢迎,没有给出原因。这是有原因的吗,我应该在意吗?
当前回答
我知道最好的做法是使用int,所有MSDN代码都使用int。然而,据我所知,除了标准化和一致性之外,没有其他原因。
其他回答
根据Visual Studio 2012中的即时窗口,Int32是int型,Int64是long型。输出如下:
sizeof(int)
4
sizeof(Int32)
4
sizeof(Int64)
8
Int32
int
base {System.ValueType}: System.ValueType
MaxValue: 2147483647
MinValue: -2147483648
Int64
long
base {System.ValueType}: System.ValueType
MaxValue: 9223372036854775807
MinValue: -9223372036854775808
int
int
base {System.ValueType}: System.ValueType
MaxValue: 2147483647
MinValue: -2147483648
ECMA-334:2006 c#语言规范(p18)
每个预定义类型都是系统提供类型的简写。例如,关键字int指向结构体System.Int32。就风格而言,使用关键字优于使用完整的系统类型名。
我总是使用系统类型——例如,Int32而不是int。我在阅读了Applied . net Framework Programming之后采用了这种做法——作者Jeffrey Richter为使用完整类型名做了一个很好的例子。以下两点让我印象深刻:
Type names can vary between .NET languages. For example, in C#, long maps to System.Int64 while in C++ with managed extensions, long maps to Int32. Since languages can be mixed-and-matched while using .NET, you can be sure that using the explicit class name will always be clearer, no matter the reader's preferred language. Many framework methods have type names as part of their method names: BinaryReader br = new BinaryReader( /* ... */ ); float val = br.ReadSingle(); // OK, but it looks a little odd... Single val = br.ReadSingle(); // OK, and is easier to read
int是c#语言中System的快捷方式。Int32
虽然这确实意味着微软可能会改变这种映射,但FogCreek讨论的一篇文章指出[来源]
“关于64位的问题——微软确实在开发64位版本的。net框架,但我很确定int不会在该系统上映射到64位。
原因:
1. c# ECMA标准明确规定int是32位,long是64位。
2. 微软在框架1.1版本中引入了额外的属性和方法,返回长值而不是int值,例如Array。除了Array.GetLength之外的GetLongLength。
所以我认为可以肯定地说,所有内置的c#类型都将保持当前的映射。”
我知道最好的做法是使用int,所有MSDN代码都使用int。然而,据我所知,除了标准化和一致性之外,没有其他原因。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和