在c#中,int和Int32是同一个东西,但我读过很多次int比Int32更受欢迎,没有给出原因。这是有原因的吗,我应该在意吗?


当前回答

你不应该关心大多数编程语言,除非你需要编写非常特定的数学函数,或者针对特定架构优化的代码……只要确保类型的大小足够你(例如,如果你知道你需要超过32位,就使用比Int更大的类型)

其他回答

int是System的别名。Int32,定义如下: 内置类型表(c#参考)

int和Int32之间没有区别,但由于int是一个语言关键字,许多人在风格上更喜欢它(就像string vs string)。

使用Int32类型需要对System或完全限定(System.Int32)的命名空间引用。我倾向于int,因为它不需要名称空间导入,因此在某些情况下减少了名称空间冲突的机会。当编译为IL时,两者之间没有区别。

int与System相同。Int32,当编译时,它将在CIL中变成相同的东西。

我们在c#中按照惯例使用int,因为c#希望看起来像C和c++(以及Java),这就是我们在那里使用的……

顺便说一句,我最终使用的是系统。Int32时声明各种Windows API函数的导入。我不确定这是否是一个定义的约定,但它提醒我,我要去一个外部DLL…

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