在c#中,int和Int32是同一个东西,但我读过很多次int比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是一个c#关键字,是明确的。

大多数情况下,这并不重要,但有两件事与Int32相悖:

您需要有一个“using System;”语句。使用"int"不需要Using语句。 可以定义自己的名为Int32的类(这将是愚蠢和令人困惑的)。Int总是Int的意思。

你不应该在乎。大多数时候你应该使用int。它将有助于将来将程序移植到更广泛的体系结构(目前int是System的别名。Int32,但这可能会改变)。只有当变量的位宽很重要时(例如:要控制结构体在内存中的布局),才应该使用int32和其他变量(与"using System;"相关联)。

你不应该在意。如果大小是一个问题,我会使用字节,短,int,然后长。使用int型比int32大的唯一原因是需要一个大于2147483647或小于-2147483648的数。

除此之外,我不在乎,还有很多其他的事情需要关心。

我总是使用系统类型——例如,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