C# 我应该使用 int 还是 Int32

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/62503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:21:01  来源:igfitidea点击:

Should I use int or Int32

提问by Graham

In C#, intand Int32are the same thing, but I've read a number of times that intis preferred over Int32with no reason given. Is there a reason, and should I care?

在C#中,int并且Int32是同样的事情,但我读了许多那个时代int优于Int32没有给出理由。有没有原因,我应该关心吗?

采纳答案by HasaniH

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

ECMA-334:2006 C# 语言规范(p18):

Each of the predefined types is shorthand for a system-provided type. For example, the keyword intrefers 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。就风格而言,使用关键字优于使用完整的系统类型名称。

回答by David Basarab

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.

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

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

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

回答by Raithlin

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。但是,据我所知,没有超出标准化和一致性的原因。

回答by James Sutherland

The two are indeed synonymous; intwill be a little more familiar looking, Int32makes the 32-bitness more explicit to those reading your code. I would be inclined to use intwhere I just need 'an integer', Int32where the size is important (cryptographic code, structures) so future maintainers will know it's safe to enlarge an intif appropriate, but should take care changing Int32s in the same way.

这两者确实是同义词。int看起来会更熟悉一点,Int32使 32 位对于阅读您的代码的人来说更加明确。我倾向于int在我只需要“整数”的Int32地方使用,其中大小很重要(密码代码、结构),因此未来的维护者会知道int在适当的情况下扩大 s 是安全的,但应该注意Int32以相同的方式更改s。

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

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

回答by Simon Steele

There is no difference between intand Int32, but as intis a language keyword many people prefer it stylistically (just as with stringvs String).

int和之间没有区别Int32,但作为int语言关键字,许多人在风格上更喜欢它(就像stringvs 一样String)。

回答by Jesper Kihlberg

intand Int32is the same. intis an alias for Int32.

int并且Int32是一样的。int是 的别名Int32

回答by Greg D

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. More familiar to the typical C# developer.
  3. A different color in the default visual studio syntax highlighting.
  1. 打字更快。
  2. 对典型的 C# 开发人员来说更熟悉。
  3. 默认 Visual Studio 语法突出显示中的不同颜色。

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

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

回答by spoulson

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

如前所述,int= Int32。为了安全起见,在实现任何关心数据类型边界的事情时,一定要始终使用int.MinValue/ int.MaxValue。假设 .NET 决定int现在是Int64,您的代码将减少对边界的依赖。

回答by Hyman Bolding

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

回答by RobbieGee

The bytes int can hold depends on what you compiled it for, so when you compile your program for 32 bit processors, it holds numbers from 2^32/2 to -2^32/2+1, while compiled for 64 bit it can hold from 2^64/2 to -2^64/2+1. int32 will always hold 2^32 values.

int 可以容纳的字节数取决于您编译它的目的,因此当您为 32 位处理器编译程序时,它包含从 2^32/2 到 -2^32/2+1 的数字,而为 64 位编译它可以保持从 2^64/2 到 -2^64/2+1。int32 将始终保存 2^32 个值。

Edit: Ignore my answer, I didn't see C#. My answer was intended for C and C++. I've never used C#

编辑:忽略我的回答,我没有看到 C#。我的回答是针对 C 和 C++ 的。我从来没有用过 C#

回答by RobbieGee

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");