我应该使用int还是Int32

时间:2021-06-22 16:05:58

In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred over Int32 with no reason given. Is there a reason, and should I care?

在C#中,int和Int32是相同的东西,但是我已经多次读过int而不是Int32,而没有给出任何理由。有原因吗,我应该关心吗?

34 个解决方案

#1


123  

ECMA-334:2006 C# Language Specification (p18):

ECMA-334:2006 C#语言规范(第18页):

Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32. As a matter of style, use of the keyword is favoured over use of the complete system type name.

每种预定义类型都是系统提供的类型的简写。例如,关键字int指的是struct System.Int32。作为一种风格问题,使用关键字比使用完整的系统类型名称更受青睐。

#2


260  

The two 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 Int32s in the same way.

这两者确实是同义词; int会更加熟悉一下,Int32使32位读取代码更加明确。我倾向于使用int,我只需要'一个整数',Int32,其中大小很重要(加密代码,结构),所以未来的维护者会知道在适当的情况下放大int是安全的,但是应该注意改变Int32s同样的方式。

The resulting code will be identical: the difference is purely one of readability or code appearance.

生成的代码将是相同的:差异纯粹是可读性或代码外观之一。

#3


83  

They both declare 32 bit integers, and as other posters stated, which one you use is mostly a matter of syntactic style. However they don't always behave the same way. For instance, the C# compiler won't allow this:

它们都声明了32位整数,并且正如其他海报所说,你使用哪一个主要是语法风格。然而,它们并不总是以相同的方式行事。例如,C#编译器不允许这样:

public enum MyEnum : Int32
{
    member1 = 0
}

but it will allow this:

但它会允许这样:

public enum MyEnum : int
{
    member1 = 0
}

Go figure.

#4


48  

I always use the system types - e.g., Int32 instead of int. I adopted this practice after reading Applied .NET Framework Programming - author Jeffrey Richter makes a good case for using the full type names. Here are the two points that stuck with me:

我总是使用系统类型 - 例如,Int32而不是int。我在阅读了应用.NET Framework编程后采用了这种做法 - 作者Jeffrey Richter为使用完整类型名称提供了一个很好的案例。以下是与我相关的两点:

  1. 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.

    类型名称可能因.NET语言而异。例如,在C#中,长映射到System.Int64,而在C ++中使用托管扩展,长映射到Int32。由于语言可以在使用.NET时进行混合匹配,因此无论读者的首选语言如何,您都可以确保使用显式类名称将更加清晰。

  2. Many framework methods have type names as part of their method names:

    许多框架方法都将类型名称作为其方法名称的一部分:

    BinaryReader br = new BinaryReader( /* ... */ );

    BinaryReader br = new BinaryReader(/ * ... * /);

    float val = br.ReadSingle(); // OK, but it looks a little odd...

    float val = br.ReadSingle(); //好的,但看起来有点奇怪......

    Single val = br.ReadSingle(); // OK, and is easier to read

    Single val = br.ReadSingle(); //好的,更容易阅读

#5


19  

int is a C# keyword and is unambiguous.

int是一个C#关键字,是明确的。

Most of the time it doesn't matter but two things that go against Int32:

大多数情况下,无关紧要的是针对Int32的两件事:

  • You need to have a "using System;" statement. using "int" requires no using statement.
  • 你需要一个“使用系统”;声明。使用“int”不需要using语句。

  • It is possible to define your own class called Int32 (which would be silly and confusing). int always means int.
  • 可以定义自己的类Int32(这将是愚蠢和混乱)。 int总是表示int。

#6


12  

As already stated, int = Int32. To be safe, be sure to always use int.MinValue/int.MaxValue when implementing anything that cares about the data type boundaries. Suppose .NET decided that int would now be Int64, your code would be less dependent on the bounds.

如前所述,int = Int32。为了安全起见,在实现关心数据类型边界的任何内容时,请务必始终使用int.MinValue / int.MaxValue。假设.NET认为int现在是Int64,你的代码将更少依赖于边界。

#7


9  

There is no difference between int and Int32, but as int is a language keyword many people prefer it stylistically (just as with string vs String).

int和Int32之间没有区别,但由于int是一个语言关键字,很多人更喜欢它的风格(就像字符串vs String一样)。

#8


9  

Byte size for types is not too interesting when you only have to deal with a single language (and for code which you don't have to remind yourself about math overflows). The part that becomes interesting is when you bridge between one language to another, C# to COM object, etc., or you're doing some bit-shifting or masking and you need to remind yourself (and your code-review co-wokers) of the size of the data.

当你只需要处理一种语言时,类型的字节大小就不那么有趣了(对于那些你不必提醒自己有关数学溢出的代码)。变得有趣的部分是当你在一种语言与另一种语言之间架起桥梁,C#与COM对象等等,或者你正在进行一些变换或掩盖,你需要提醒自己(以及你的代码审查共同工作者)的数据大小。

In practice, I usually use Int32 just to remind myself what size they are because I do write managed C++ (to bridge to C# for example) as well as unmanaged/native C++.

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

Long as you probably know, in C# is 64-bits, but in native C++, it ends up as 32-bits, or char is unicode/16-bits while in C++ it is 8-bits. But how do we know this? The answer is, because we've looked it up in the manual and it said so.

只要你可能知道,在C#中是64位,但在本机C ++中,它最终为32位,或者char是unicode / 16位,而在C ++中它是8位。但我们怎么知道呢?答案是,因为我们在手册中进行了查阅,并且它是这么说的。

With time and experiences, you will start to be more type-conscientious when you do write codes to bridge between C# and other languages (some readers here are thinking "why would you?"), but IMHO I believe it is a better practice because I cannot remember what I've coded last week (or I don't have to specify in my API document that "this parameter is 32-bits integer").

有了时间和经验,当你编写代码以便在C#和其他语言之间架起桥梁时,你会开始变得更加认真(这里的一些读者正在思考“为什么会这样?”),但恕我直言,我认为这是一种更好的做法,因为我不记得上周我编写的内容(或者我不必在我的API文档中指定“此参数是32位整数”)。

In F# (although I've never used it), they define int, int32, and nativeint. The same question should rise, "which one do I use?". As others has mentioned, in most cases, it should not matter (should be transparent). But I for one would choose int32 and uint32 just to remove the ambiguities.

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

I guess it would just depend on what applications you are coding, who's using it, what coding practices you and your team follows, etc. to justify when to use Int32.

我想这将取决于您编码的应用程序,使用者,您和您的团队遵循的编码实践等,以证明何时使用Int32。

#9


7  

In my experience it's been a convention thing. I'm not aware of any technical reason to use int over Int32, but it's:

根据我的经验,这是一个常规的事情。我不知道在Int32上使用int的任何技术原因,但它是:

  1. Quicker to type.
  2. 键入更快。

  3. More familiar to the typical C# developer.
  4. 对典型的C#开发人员比较熟悉。

  5. A different color in the default visual studio syntax highlighting.
  6. 默认的visual studio语法高亮显示中的不同颜色。

I'm especially fond of that last one. :)

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

#10


6  

I always use the aliased types (int, string, etc.) when defining a variable and use the real name when accessing a static method:

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

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

It just seems ugly to see something like int.TryParse(). There's no other reason I do this other than style.

看到类似int.TryParse()的东西似乎很难看。除了风格之外别无其他原因。

#11


5  

I know that the best practice is to use int, and all MSDN code uses int. However, there's not a reason beyond standardisation and consistency as far as I know.

我知道最好的做法是使用int,所有MSDN代码都使用int。但是,据我所知,没有超出标准化和一致性的理由。

#12


5  

Though they are (mostly) identical (see below for the one [bug] difference), you definitely should care and you should use Int32.

虽然它们(大多数)是相同的(参见下面的一个[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?

    Int-16的整数名称为Int16。对于64位整数,它是Int64,对于32位整数,直观的选择是:int或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).

    Int16,Int32或Int64类型的变量大小的问题是自引用的,但int类型变量大小的问题是一个完全有效的问题,无论多么微不足道,问题都会让人分心,导致混淆,浪费时间,阻碍讨论等(这个问题存在的事实证明了这一点)。

  • 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".

    使用Int32可以促使开发人员意识到他们选择的类型。再一次int有多大?哦是的,32。当名称中包含大小时,实际考虑类型大小的可能性更大。使用Int32还可以提升对其他选择的了解。当人们不*至少认识到有替代品时,int变得太容易变成“整数型”。

  • 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?

    用于与32位整数交互的框架内的类名为Int32。再一次,这是:更直观,更少混淆,缺乏(不必要的)翻译(不是系统中的翻译,但是在开发人员的脑海中)等.int lMax = Int32.MaxValue或Int32 lMax = Int32.MaxValue ?

  • int isn't a keyword in all .NET languages.

    int不是所有.NET语言中的关键字。

  • Although there are arguments why it's not likely to ever change, int may not always be an Int32.

    虽然有争议为什么它不可能永远不会改变,但int可能并不总是Int32。

The drawbacks are two extra characters to type and [bug].

缺点是键入两个额外字符和[bug]。

This won't compile

这不会编译

public enum MyEnum : Int32
{
    AEnum = 0
}

But this will:

但这会:

public enum MyEnum : int
{
    AEnum = 0
}

#13


4  

You shouldn't care. You should use int most of the time. It will help the porting of your program to a wider architecture in the future (currently int is an alias to System.Int32 but that could change). Only when the bit width of the variable matters (for instance: to control the layout in memory of a struct) you should use int32 and others (with the associated "using System;").

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

#14


3  

int is the C# language's shortcut for System.Int32

int是C#语言的System.Int32快捷方式

Whilst this does mean that Microsoft could change this mapping, a post on FogCreek's discussions stated [source]

虽然这确实意味着微软可以改变这种映射,但有关FogCreek讨论的帖子[来源]

"On the 64 bit issue -- Microsoft is indeed working on a 64-bit version of the .NET Framework but I'm pretty sure int will NOT map to 64 bit on that system.

“在64位问题上 - 微软确实在开发64位版本的.NET Framework,但我很确定int不会映射到该系统上的64位。

Reasons:

1. The C# ECMA standard specifically says that int is 32 bit and long is 64 bit.

1. C#ECMA标准明确指出int为32位,long为64位。

2. Microsoft introduced additional properties & methods in Framework version 1.1 that return long values instead of int values, such as Array.GetLongLength in addition to Array.GetLength.

2. Microsoft在Framework 1.1版中引入了其他属性和方法,这些属性和方法返回long值而不是int值,例如Array.GetLongLength以及Array.GetLength。

So I think it's safe to say that all built-in C# types will keep their current mapping."

所以我认为可以肯定地说所有内置的C#类型都会保留它们当前的映射。“

#15


3  

int is the same as System.Int32 and when compiled it will turn into the same thing in CIL.

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

We use int by convention in C# since C# wants to look like C and C++ (and Java) and that is what we use there...

我们在C#中按惯例使用int,因为C#想要看起来像C和C ++(和Java),这就是我们在那里使用的...

BTW, I do end up using System.Int32 when declaring imports of various Windows API functions. I am not sure if this is a defined convention or not, but it reminds me that I am going to an external DLL...

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

#16


3  

Once upon a time, the int datatype was pegged to the register size of the machine targeted by the compiler. So, for example, a compiler for a 16-bit system would use a 16-bit integer.

曾几何时,int数据类型与编译器所针对的机器的寄存器大小挂钩。因此,例如,16位系统的编译器将使用16位整数。

However, we thankfully don't see much 16-bit any more, and when 64-bit started to get popular people were more concerned with making it compatible with older software and 32-bit had been around so long that for most compilers an int is just assumed to be 32 bits.

但是,幸运的是,我们还没有看到更多的16位,当64位开始受欢迎时,人们更关心的是让它与旧软件兼容,32位已经存在了很长时间以至于对于大多数编译器而言假设是32位。

#17


3  

I'd recommend using Microsoft's StyleCop.

我建议使用微软的StyleCop。

It is like FxCop, but for style-related issues. The default configuration matches Microsoft's internal style guides, but it can be customised for your project.

它就像FxCop,但与风格相关的问题。默认配置与Microsoft的内部样式指南相匹配,但可以为您的项目自定义。

It can take a bit to get used to, but it definitely makes your code nicer.

它可能需要一些时间来习惯,但它肯定会使你的代码更好。

You can include it in your build process to automatically check for violations.

您可以将其包含在构建过​​程中以自动检查违规。

#18


2  

It makes no difference in practice and in time you will adopt your own convention. I tend to use the keyword when assigning a type, and the class version when using static methods and such:

它在实践中没有任何区别,并且您将采用自己的惯例。我倾向于在分配类型时使用关键字,而在使用静态方法时使用类版本等:

int total = Int32.Parse("1009");

int total = Int32.Parse(“1009”);

#19


2  

int and Int32 is the same. int is an alias for Int32.

int和Int32是一样的。 int是Int32的别名。

#20


2  

You should not care. If size is a concern I would use byte, short, int, then long. The only reason you would use an int larger than int32 is if you need a number higher than 2147483647 or lower than -2147483648.

你不应该在乎。如果大小是一个问题,我会使用byte,short,int,然后long。使用大于int32的int的唯一原因是,如果需要高于2147483647或低于-2147483648的数字。

Other than that I wouldn't care, there are plenty of other items to be concerned with.

除了我不在乎之外,还有很多其他项目需要关注。

#21


1  

int is an alias for System.Int32, as defined in this table: Built-In Types Table (C# Reference)

int是System.Int32的别名,如下表中所定义:内置类型表(C#参考)

#22


1  

I use int in the event that Microsoft changes the default implementation for an integer to some new fangled version (let's call it Int32b).

如果Microsoft将整数的默认实现更改为某个新的fangled版本(我们称之为Int32b),我使用int。

Microsoft can then change the int alias to Int32b, and I don't have to change any of my code to take advantage of their new (and hopefully improved) integer implementation.

然后,Microsoft可以将int别名更改为Int32b,并且我不必更改任何代码以利用其新的(并且希望改进的)整数实现。

The same goes for any of the type keywords.

任何类型关键字都是如此。

#23


0  

You should not care in most programming languages, unless you need to write very specific mathematical functions, or code optimized for one specific architecture... Just make sure the size of the type is enough for you (use something bigger than an Int if you know you'll need more than 32-bits for example)

您不应该关心大多数编程语言,除非您需要编写非常具体的数学函数或针对特定体系结构优化的代码...只需确保类型的大小对您来说足够(如果您使用大于Int的东西)知道你需要超过32位的例子)

#24


0  

It doesn't matter. int is the language keyword and Int32 its actual system type.

没关系。 int是语言关键字,Int32是其实际系统类型。

See also my answer here to a related question.

另请参阅我在这里回答相关问题。

#25


0  

Use of Int or Int32 are the same Int is just sugar to simplify the code for the reader.

使用Int或Int32是相同的Int只是糖来简化读者的代码。

Use the Nullable variant Int? or Int32? when you work with databases on fields containing null. That will save you from a lot of runtime issues.

使用Nullable变体Int?还是Int32?在包含null的字段上使用数据库时。这样可以避免许多运行时问题。

#26


0  

Some compilers have different sizes for int on different platforms (not C# specific)

一些编译器在不同平台上具有不同的int大小(不是C#特定的)

Some coding standards (MISRA C) requires that all types used are size specified (i.e. Int32 and not int).

某些编码标准(MISRA C)要求使用的所有类型都是指定的大小(即Int32而不是int)。

It is also good to specify prefixes for different type variables (e.g. b for 8 bit byte, w for 16 bit word, and l for 32 bit long word => Int32 lMyVariable)

为不同类型变量指定前缀也很好(例如,b表示8位字节,w表示16位字,l表示32位长字=> Int32 lMyVariable)

You should care because it makes your code more portable and more maintainable.

你应该小心,因为它使你的代码更便携,更易于维护。

Portable may not be applicable to C# if you are always going to use C# and the C# specification will never change in this regard.

如果您总是要使用C#,则Portable可能不适用于C#,并且C#规范在这方面永远不会改变。

Maintainable ihmo will always be applicable, because the person maintaining your code may not be aware of this particular C# specification, and miss a bug were the int occasionaly becomes more than 2147483647.

可维护的ihmo将始终适用,因为维护代码的人可能不知道这个特定的C#规范,并且错过了一个错误,因为int偶尔会超过2147483647。

In a simple for-loop that counts for example the months of the year, you won't care, but when you use the variable in a context where it could possibly owerflow, you should care.

在一个简单的for循环中,例如一年中的几个月,你不会在乎,但是当你在可能有能力的上下文中使用变量时,你应该关心。

You should also care if you are going to do bit-wise operations on it.

您还应该关心是否要对其进行逐位操作。

#27


0  

Using the Int32 type requires a namespace reference to System, or fully qualifying (System.Int32). I tend toward int, because it doesn't require a namespace import, therefore reducing the chance of namespace collision in some cases. When compiled to IL, there is no difference between the two.

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

#28


0  

According to the Immediate Window in Visual Studio 2012 Int32 is int, Int64 is long. Here is the output:

根据Visual Studio 2012中的立即窗口Int32是int,Int64很长。这是输出:

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

#29


0  

Also consider Int16. If you need to store an Integer in memory in your application and you are concerned about the amount of memory used, then you could go with Int16 since it uses less memeory and has a smaller min/max range than Int32 (which is what int is.)

还要考虑Int16。如果你需要在你的应用程序中将一个Integer存储在内存中并且你担心使用的内存量,那么你可以使用Int16,因为它使用更少的内存并且具有比Int32更小的最小/最大范围(这是int是什么。)

#30


0  

A while back I was working on a project with Microsoft when we had a visit from someone on the Microsoft .NET CLR product team. This person coded examples and when he defined his variables he used “Int32” vs. “int” and “String” vs. “string”.

不久前,当我们访问Microsoft .NET CLR产品团队的某个人时,我正在与Microsoft合作开展一个项目。这个人编码的例子,当他定义他的变量时,他使用“Int32”与“int”和“String”与“string”。

I had remembered seeing this style in other example code from Microsoft. So, I did some research and found that everyone says that there is no difference between the “Int32” and “int” except for syntax coloring. In fact, I found a lot of material suggesting you use “Int32” to make your code more readable. So, I adopted the style.

我记得在微软的其他示例代码中看到了这种风格。所以,我做了一些研究,发现每个人都说除了语法着色之外,“Int32”和“int”之间没有区别。事实上,我发现很多材料建议您使用“Int32”来使您的代码更具可读性。所以,我采用了这种风格。

The other day I did find a difference! The compiler doesn’t allow you to type enum using the “Int32”, but it does when you use “int”. Don’t ask me why because I don’t know yet.

前几天我确实发现了一个不同!编译器不允许您使用“Int32”键入枚举,但是当您使用“int”时它会执行。不要问我为什么,因为我还不知道。

Example:

public  enum MyEnum : Int32
{
    AEnum = 0
}

This works.

public enum MyEnum : int
{
    AEnum = 0
}

Taken from: Int32 notation vs. int

取自:Int32符号与int

#1


123  

ECMA-334:2006 C# Language Specification (p18):

ECMA-334:2006 C#语言规范(第18页):

Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32. As a matter of style, use of the keyword is favoured over use of the complete system type name.

每种预定义类型都是系统提供的类型的简写。例如,关键字int指的是struct System.Int32。作为一种风格问题,使用关键字比使用完整的系统类型名称更受青睐。

#2


260  

The two 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 Int32s in the same way.

这两者确实是同义词; int会更加熟悉一下,Int32使32位读取代码更加明确。我倾向于使用int,我只需要'一个整数',Int32,其中大小很重要(加密代码,结构),所以未来的维护者会知道在适当的情况下放大int是安全的,但是应该注意改变Int32s同样的方式。

The resulting code will be identical: the difference is purely one of readability or code appearance.

生成的代码将是相同的:差异纯粹是可读性或代码外观之一。

#3


83  

They both declare 32 bit integers, and as other posters stated, which one you use is mostly a matter of syntactic style. However they don't always behave the same way. For instance, the C# compiler won't allow this:

它们都声明了32位整数,并且正如其他海报所说,你使用哪一个主要是语法风格。然而,它们并不总是以相同的方式行事。例如,C#编译器不允许这样:

public enum MyEnum : Int32
{
    member1 = 0
}

but it will allow this:

但它会允许这样:

public enum MyEnum : int
{
    member1 = 0
}

Go figure.

#4


48  

I always use the system types - e.g., Int32 instead of int. I adopted this practice after reading Applied .NET Framework Programming - author Jeffrey Richter makes a good case for using the full type names. Here are the two points that stuck with me:

我总是使用系统类型 - 例如,Int32而不是int。我在阅读了应用.NET Framework编程后采用了这种做法 - 作者Jeffrey Richter为使用完整类型名称提供了一个很好的案例。以下是与我相关的两点:

  1. 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.

    类型名称可能因.NET语言而异。例如,在C#中,长映射到System.Int64,而在C ++中使用托管扩展,长映射到Int32。由于语言可以在使用.NET时进行混合匹配,因此无论读者的首选语言如何,您都可以确保使用显式类名称将更加清晰。

  2. Many framework methods have type names as part of their method names:

    许多框架方法都将类型名称作为其方法名称的一部分:

    BinaryReader br = new BinaryReader( /* ... */ );

    BinaryReader br = new BinaryReader(/ * ... * /);

    float val = br.ReadSingle(); // OK, but it looks a little odd...

    float val = br.ReadSingle(); //好的,但看起来有点奇怪......

    Single val = br.ReadSingle(); // OK, and is easier to read

    Single val = br.ReadSingle(); //好的,更容易阅读

#5


19  

int is a C# keyword and is unambiguous.

int是一个C#关键字,是明确的。

Most of the time it doesn't matter but two things that go against Int32:

大多数情况下,无关紧要的是针对Int32的两件事:

  • You need to have a "using System;" statement. using "int" requires no using statement.
  • 你需要一个“使用系统”;声明。使用“int”不需要using语句。

  • It is possible to define your own class called Int32 (which would be silly and confusing). int always means int.
  • 可以定义自己的类Int32(这将是愚蠢和混乱)。 int总是表示int。

#6


12  

As already stated, int = Int32. To be safe, be sure to always use int.MinValue/int.MaxValue when implementing anything that cares about the data type boundaries. Suppose .NET decided that int would now be Int64, your code would be less dependent on the bounds.

如前所述,int = Int32。为了安全起见,在实现关心数据类型边界的任何内容时,请务必始终使用int.MinValue / int.MaxValue。假设.NET认为int现在是Int64,你的代码将更少依赖于边界。

#7


9  

There is no difference between int and Int32, but as int is a language keyword many people prefer it stylistically (just as with string vs String).

int和Int32之间没有区别,但由于int是一个语言关键字,很多人更喜欢它的风格(就像字符串vs String一样)。

#8


9  

Byte size for types is not too interesting when you only have to deal with a single language (and for code which you don't have to remind yourself about math overflows). The part that becomes interesting is when you bridge between one language to another, C# to COM object, etc., or you're doing some bit-shifting or masking and you need to remind yourself (and your code-review co-wokers) of the size of the data.

当你只需要处理一种语言时,类型的字节大小就不那么有趣了(对于那些你不必提醒自己有关数学溢出的代码)。变得有趣的部分是当你在一种语言与另一种语言之间架起桥梁,C#与COM对象等等,或者你正在进行一些变换或掩盖,你需要提醒自己(以及你的代码审查共同工作者)的数据大小。

In practice, I usually use Int32 just to remind myself what size they are because I do write managed C++ (to bridge to C# for example) as well as unmanaged/native C++.

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

Long as you probably know, in C# is 64-bits, but in native C++, it ends up as 32-bits, or char is unicode/16-bits while in C++ it is 8-bits. But how do we know this? The answer is, because we've looked it up in the manual and it said so.

只要你可能知道,在C#中是64位,但在本机C ++中,它最终为32位,或者char是unicode / 16位,而在C ++中它是8位。但我们怎么知道呢?答案是,因为我们在手册中进行了查阅,并且它是这么说的。

With time and experiences, you will start to be more type-conscientious when you do write codes to bridge between C# and other languages (some readers here are thinking "why would you?"), but IMHO I believe it is a better practice because I cannot remember what I've coded last week (or I don't have to specify in my API document that "this parameter is 32-bits integer").

有了时间和经验,当你编写代码以便在C#和其他语言之间架起桥梁时,你会开始变得更加认真(这里的一些读者正在思考“为什么会这样?”),但恕我直言,我认为这是一种更好的做法,因为我不记得上周我编写的内容(或者我不必在我的API文档中指定“此参数是32位整数”)。

In F# (although I've never used it), they define int, int32, and nativeint. The same question should rise, "which one do I use?". As others has mentioned, in most cases, it should not matter (should be transparent). But I for one would choose int32 and uint32 just to remove the ambiguities.

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

I guess it would just depend on what applications you are coding, who's using it, what coding practices you and your team follows, etc. to justify when to use Int32.

我想这将取决于您编码的应用程序,使用者,您和您的团队遵循的编码实践等,以证明何时使用Int32。

#9


7  

In my experience it's been a convention thing. I'm not aware of any technical reason to use int over Int32, but it's:

根据我的经验,这是一个常规的事情。我不知道在Int32上使用int的任何技术原因,但它是:

  1. Quicker to type.
  2. 键入更快。

  3. More familiar to the typical C# developer.
  4. 对典型的C#开发人员比较熟悉。

  5. A different color in the default visual studio syntax highlighting.
  6. 默认的visual studio语法高亮显示中的不同颜色。

I'm especially fond of that last one. :)

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

#10


6  

I always use the aliased types (int, string, etc.) when defining a variable and use the real name when accessing a static method:

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

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

It just seems ugly to see something like int.TryParse(). There's no other reason I do this other than style.

看到类似int.TryParse()的东西似乎很难看。除了风格之外别无其他原因。

#11


5  

I know that the best practice is to use int, and all MSDN code uses int. However, there's not a reason beyond standardisation and consistency as far as I know.

我知道最好的做法是使用int,所有MSDN代码都使用int。但是,据我所知,没有超出标准化和一致性的理由。

#12


5  

Though they are (mostly) identical (see below for the one [bug] difference), you definitely should care and you should use Int32.

虽然它们(大多数)是相同的(参见下面的一个[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?

    Int-16的整数名称为Int16。对于64位整数,它是Int64,对于32位整数,直观的选择是:int或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).

    Int16,Int32或Int64类型的变量大小的问题是自引用的,但int类型变量大小的问题是一个完全有效的问题,无论多么微不足道,问题都会让人分心,导致混淆,浪费时间,阻碍讨论等(这个问题存在的事实证明了这一点)。

  • 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".

    使用Int32可以促使开发人员意识到他们选择的类型。再一次int有多大?哦是的,32。当名称中包含大小时,实际考虑类型大小的可能性更大。使用Int32还可以提升对其他选择的了解。当人们不*至少认识到有替代品时,int变得太容易变成“整数型”。

  • 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?

    用于与32位整数交互的框架内的类名为Int32。再一次,这是:更直观,更少混淆,缺乏(不必要的)翻译(不是系统中的翻译,但是在开发人员的脑海中)等.int lMax = Int32.MaxValue或Int32 lMax = Int32.MaxValue ?

  • int isn't a keyword in all .NET languages.

    int不是所有.NET语言中的关键字。

  • Although there are arguments why it's not likely to ever change, int may not always be an Int32.

    虽然有争议为什么它不可能永远不会改变,但int可能并不总是Int32。

The drawbacks are two extra characters to type and [bug].

缺点是键入两个额外字符和[bug]。

This won't compile

这不会编译

public enum MyEnum : Int32
{
    AEnum = 0
}

But this will:

但这会:

public enum MyEnum : int
{
    AEnum = 0
}

#13


4  

You shouldn't care. You should use int most of the time. It will help the porting of your program to a wider architecture in the future (currently int is an alias to System.Int32 but that could change). Only when the bit width of the variable matters (for instance: to control the layout in memory of a struct) you should use int32 and others (with the associated "using System;").

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

#14


3  

int is the C# language's shortcut for System.Int32

int是C#语言的System.Int32快捷方式

Whilst this does mean that Microsoft could change this mapping, a post on FogCreek's discussions stated [source]

虽然这确实意味着微软可以改变这种映射,但有关FogCreek讨论的帖子[来源]

"On the 64 bit issue -- Microsoft is indeed working on a 64-bit version of the .NET Framework but I'm pretty sure int will NOT map to 64 bit on that system.

“在64位问题上 - 微软确实在开发64位版本的.NET Framework,但我很确定int不会映射到该系统上的64位。

Reasons:

1. The C# ECMA standard specifically says that int is 32 bit and long is 64 bit.

1. C#ECMA标准明确指出int为32位,long为64位。

2. Microsoft introduced additional properties & methods in Framework version 1.1 that return long values instead of int values, such as Array.GetLongLength in addition to Array.GetLength.

2. Microsoft在Framework 1.1版中引入了其他属性和方法,这些属性和方法返回long值而不是int值,例如Array.GetLongLength以及Array.GetLength。

So I think it's safe to say that all built-in C# types will keep their current mapping."

所以我认为可以肯定地说所有内置的C#类型都会保留它们当前的映射。“

#15


3  

int is the same as System.Int32 and when compiled it will turn into the same thing in CIL.

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

We use int by convention in C# since C# wants to look like C and C++ (and Java) and that is what we use there...

我们在C#中按惯例使用int,因为C#想要看起来像C和C ++(和Java),这就是我们在那里使用的...

BTW, I do end up using System.Int32 when declaring imports of various Windows API functions. I am not sure if this is a defined convention or not, but it reminds me that I am going to an external DLL...

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

#16


3  

Once upon a time, the int datatype was pegged to the register size of the machine targeted by the compiler. So, for example, a compiler for a 16-bit system would use a 16-bit integer.

曾几何时,int数据类型与编译器所针对的机器的寄存器大小挂钩。因此,例如,16位系统的编译器将使用16位整数。

However, we thankfully don't see much 16-bit any more, and when 64-bit started to get popular people were more concerned with making it compatible with older software and 32-bit had been around so long that for most compilers an int is just assumed to be 32 bits.

但是,幸运的是,我们还没有看到更多的16位,当64位开始受欢迎时,人们更关心的是让它与旧软件兼容,32位已经存在了很长时间以至于对于大多数编译器而言假设是32位。

#17


3  

I'd recommend using Microsoft's StyleCop.

我建议使用微软的StyleCop。

It is like FxCop, but for style-related issues. The default configuration matches Microsoft's internal style guides, but it can be customised for your project.

它就像FxCop,但与风格相关的问题。默认配置与Microsoft的内部样式指南相匹配,但可以为您的项目自定义。

It can take a bit to get used to, but it definitely makes your code nicer.

它可能需要一些时间来习惯,但它肯定会使你的代码更好。

You can include it in your build process to automatically check for violations.

您可以将其包含在构建过​​程中以自动检查违规。

#18


2  

It makes no difference in practice and in time you will adopt your own convention. I tend to use the keyword when assigning a type, and the class version when using static methods and such:

它在实践中没有任何区别,并且您将采用自己的惯例。我倾向于在分配类型时使用关键字,而在使用静态方法时使用类版本等:

int total = Int32.Parse("1009");

int total = Int32.Parse(“1009”);

#19


2  

int and Int32 is the same. int is an alias for Int32.

int和Int32是一样的。 int是Int32的别名。

#20


2  

You should not care. If size is a concern I would use byte, short, int, then long. The only reason you would use an int larger than int32 is if you need a number higher than 2147483647 or lower than -2147483648.

你不应该在乎。如果大小是一个问题,我会使用byte,short,int,然后long。使用大于int32的int的唯一原因是,如果需要高于2147483647或低于-2147483648的数字。

Other than that I wouldn't care, there are plenty of other items to be concerned with.

除了我不在乎之外,还有很多其他项目需要关注。

#21


1  

int is an alias for System.Int32, as defined in this table: Built-In Types Table (C# Reference)

int是System.Int32的别名,如下表中所定义:内置类型表(C#参考)

#22


1  

I use int in the event that Microsoft changes the default implementation for an integer to some new fangled version (let's call it Int32b).

如果Microsoft将整数的默认实现更改为某个新的fangled版本(我们称之为Int32b),我使用int。

Microsoft can then change the int alias to Int32b, and I don't have to change any of my code to take advantage of their new (and hopefully improved) integer implementation.

然后,Microsoft可以将int别名更改为Int32b,并且我不必更改任何代码以利用其新的(并且希望改进的)整数实现。

The same goes for any of the type keywords.

任何类型关键字都是如此。

#23


0  

You should not care in most programming languages, unless you need to write very specific mathematical functions, or code optimized for one specific architecture... Just make sure the size of the type is enough for you (use something bigger than an Int if you know you'll need more than 32-bits for example)

您不应该关心大多数编程语言,除非您需要编写非常具体的数学函数或针对特定体系结构优化的代码...只需确保类型的大小对您来说足够(如果您使用大于Int的东西)知道你需要超过32位的例子)

#24


0  

It doesn't matter. int is the language keyword and Int32 its actual system type.

没关系。 int是语言关键字,Int32是其实际系统类型。

See also my answer here to a related question.

另请参阅我在这里回答相关问题。

#25


0  

Use of Int or Int32 are the same Int is just sugar to simplify the code for the reader.

使用Int或Int32是相同的Int只是糖来简化读者的代码。

Use the Nullable variant Int? or Int32? when you work with databases on fields containing null. That will save you from a lot of runtime issues.

使用Nullable变体Int?还是Int32?在包含null的字段上使用数据库时。这样可以避免许多运行时问题。

#26


0  

Some compilers have different sizes for int on different platforms (not C# specific)

一些编译器在不同平台上具有不同的int大小(不是C#特定的)

Some coding standards (MISRA C) requires that all types used are size specified (i.e. Int32 and not int).

某些编码标准(MISRA C)要求使用的所有类型都是指定的大小(即Int32而不是int)。

It is also good to specify prefixes for different type variables (e.g. b for 8 bit byte, w for 16 bit word, and l for 32 bit long word => Int32 lMyVariable)

为不同类型变量指定前缀也很好(例如,b表示8位字节,w表示16位字,l表示32位长字=> Int32 lMyVariable)

You should care because it makes your code more portable and more maintainable.

你应该小心,因为它使你的代码更便携,更易于维护。

Portable may not be applicable to C# if you are always going to use C# and the C# specification will never change in this regard.

如果您总是要使用C#,则Portable可能不适用于C#,并且C#规范在这方面永远不会改变。

Maintainable ihmo will always be applicable, because the person maintaining your code may not be aware of this particular C# specification, and miss a bug were the int occasionaly becomes more than 2147483647.

可维护的ihmo将始终适用,因为维护代码的人可能不知道这个特定的C#规范,并且错过了一个错误,因为int偶尔会超过2147483647。

In a simple for-loop that counts for example the months of the year, you won't care, but when you use the variable in a context where it could possibly owerflow, you should care.

在一个简单的for循环中,例如一年中的几个月,你不会在乎,但是当你在可能有能力的上下文中使用变量时,你应该关心。

You should also care if you are going to do bit-wise operations on it.

您还应该关心是否要对其进行逐位操作。

#27


0  

Using the Int32 type requires a namespace reference to System, or fully qualifying (System.Int32). I tend toward int, because it doesn't require a namespace import, therefore reducing the chance of namespace collision in some cases. When compiled to IL, there is no difference between the two.

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

#28


0  

According to the Immediate Window in Visual Studio 2012 Int32 is int, Int64 is long. Here is the output:

根据Visual Studio 2012中的立即窗口Int32是int,Int64很长。这是输出:

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

#29


0  

Also consider Int16. If you need to store an Integer in memory in your application and you are concerned about the amount of memory used, then you could go with Int16 since it uses less memeory and has a smaller min/max range than Int32 (which is what int is.)

还要考虑Int16。如果你需要在你的应用程序中将一个Integer存储在内存中并且你担心使用的内存量,那么你可以使用Int16,因为它使用更少的内存并且具有比Int32更小的最小/最大范围(这是int是什么。)

#30


0  

A while back I was working on a project with Microsoft when we had a visit from someone on the Microsoft .NET CLR product team. This person coded examples and when he defined his variables he used “Int32” vs. “int” and “String” vs. “string”.

不久前,当我们访问Microsoft .NET CLR产品团队的某个人时,我正在与Microsoft合作开展一个项目。这个人编码的例子,当他定义他的变量时,他使用“Int32”与“int”和“String”与“string”。

I had remembered seeing this style in other example code from Microsoft. So, I did some research and found that everyone says that there is no difference between the “Int32” and “int” except for syntax coloring. In fact, I found a lot of material suggesting you use “Int32” to make your code more readable. So, I adopted the style.

我记得在微软的其他示例代码中看到了这种风格。所以,我做了一些研究,发现每个人都说除了语法着色之外,“Int32”和“int”之间没有区别。事实上,我发现很多材料建议您使用“Int32”来使您的代码更具可读性。所以,我采用了这种风格。

The other day I did find a difference! The compiler doesn’t allow you to type enum using the “Int32”, but it does when you use “int”. Don’t ask me why because I don’t know yet.

前几天我确实发现了一个不同!编译器不允许您使用“Int32”键入枚举,但是当您使用“int”时它会执行。不要问我为什么,因为我还不知道。

Example:

public  enum MyEnum : Int32
{
    AEnum = 0
}

This works.

public enum MyEnum : int
{
    AEnum = 0
}

Taken from: Int32 notation vs. int

取自:Int32符号与int