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


当前回答

虽然它们(大部分)是相同的(参见下面的一个[错误]差异),但您绝对应该注意并应该使用Int32。

The name for a 16-bit integer is Int16. For a 64 bit integer it's Int64, and for a 32-bit integer the intuitive choice is: int or Int32? The question of the size of a variable of type Int16, Int32, or Int64 is self-referencing, but the question of the size of a variable of type int is a perfectly valid question and questions, no matter how trivial, are distracting, lead to confusion, waste time, hinder discussion, etc. (the fact this question exists proves the point). Using Int32 promotes that the developer is conscious of their choice of type. How big is an int again? Oh yeah, 32. The likelihood that the size of the type will actually be considered is greater when the size is included in the name. Using Int32 also promotes knowledge of the other choices. When people aren't forced to at least recognize there are alternatives it become far too easy for int to become "THE integer type". The class within the framework intended to interact with 32-bit integers is named Int32. Once again, which is: more intuitive, less confusing, lacks an (unnecessary) translation (not a translation in the system, but in the mind of the developer), etc. int lMax = Int32.MaxValue or Int32 lMax = Int32.MaxValue? int isn't a keyword in all .NET languages. Although there are arguments why it's not likely to ever change, int may not always be an Int32.

缺点是要多输入两个字符和[bug]。

这不会编译

public enum MyEnum : Int32
{
    AEnum = 0
}

但这将:

public enum MyEnum : int
{
    AEnum = 0
}

其他回答

当您只需要处理一种语言时,类型的字节大小就不太有趣了(对于那些不需要提醒自己数学溢出的代码)。有趣的部分是当你在一种语言和另一种语言之间建立桥梁,c#到COM对象,等等,或者你正在做一些位转移或屏蔽,你需要提醒自己(和你的代码审查同事)数据的大小。

在实践中,我通常使用Int32来提醒自己它们的大小,因为我确实写托管c++(例如桥接到c#)以及非托管/本机c++。

你可能知道,在c#中是64位,但在原生c++中,它最终是32位,或者char是unicode/16位,而在c++中是8位。但是我们是怎么知道的呢?答案是,因为我们已经在手册上查过了,上面是这么说的。

随着时间的推移和经验的积累,当你在c#和其他语言之间编写代码时,你会开始更加注重类型(这里的一些读者会想“为什么你会这样做?”),但恕我直言,我认为这是一个更好的实践,因为我不记得我上周写了什么(或者我不必在我的API文档中指定“此参数是32位整数”)。

在f#中(尽管我从未使用过它),它们定义了int, int32和nativeint。同样的问题也会出现,“我该用哪一个?”正如其他人所提到的,在大多数情况下,它不应该是重要的(应该是透明的)。但我个人会选择int32和uint32来消除歧义。

我想这取决于你在编写什么应用程序,谁在使用它,你和你的团队遵循什么编码实践,等等,来证明什么时候使用Int32。

附录: 顺便说一句,自从我几年前回答了这个问题之后,我就开始同时使用f#和Rust了。f#,它都是关于类型推断,以及c#和f#之间的桥接/互操作,本机类型匹配,所以不用担心;我很少需要在f#中显式地定义类型(如果不使用类型推断,这几乎是一种罪过)。在Rust中,他们完全消除了这种歧义,你必须使用i32 vs u32;总而言之,减少歧义有助于减少bug。

虽然它们(大部分)是相同的(参见下面的一个[错误]差异),但您绝对应该注意并应该使用Int32。

The name for a 16-bit integer is Int16. For a 64 bit integer it's Int64, and for a 32-bit integer the intuitive choice is: int or Int32? The question of the size of a variable of type Int16, Int32, or Int64 is self-referencing, but the question of the size of a variable of type int is a perfectly valid question and questions, no matter how trivial, are distracting, lead to confusion, waste time, hinder discussion, etc. (the fact this question exists proves the point). Using Int32 promotes that the developer is conscious of their choice of type. How big is an int again? Oh yeah, 32. The likelihood that the size of the type will actually be considered is greater when the size is included in the name. Using Int32 also promotes knowledge of the other choices. When people aren't forced to at least recognize there are alternatives it become far too easy for int to become "THE integer type". The class within the framework intended to interact with 32-bit integers is named Int32. Once again, which is: more intuitive, less confusing, lacks an (unnecessary) translation (not a translation in the system, but in the mind of the developer), etc. int lMax = Int32.MaxValue or Int32 lMax = Int32.MaxValue? int isn't a keyword in all .NET languages. Although there are arguments why it's not likely to ever change, int may not always be an Int32.

缺点是要多输入两个字符和[bug]。

这不会编译

public enum MyEnum : Int32
{
    AEnum = 0
}

但这将:

public enum MyEnum : int
{
    AEnum = 0
}

根据我的经验,这是一种惯例。我不知道在Int32上使用int的任何技术原因,但它是:

打字更快。 对于典型的c#开发人员来说更加熟悉。 默认visual studio语法高亮显示的不同颜色。

我特别喜欢最后一个。:)

我总是在定义变量时使用别名类型(int, string等),在访问静态方法时使用真实名称:

int x, y;
...
String.Format ("{0}x{1}", x, y);

看到像int.TryParse()这样的东西似乎很难看。除了风格,我这么做没有别的原因。

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