C#中的Int32 vs. Int64 vs. Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1680111/
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-06 19:57:53  来源:igfitidea点击:

Int32 vs. Int64 vs. Int in C#

c#.net

提问by Faruz

Possible Duplicate:
Assuming 32bit ints

可能的重复:
假设 32 位整数

So I read somewhere that int equals int32 in c#. Is it true also on 64-bit machines? Should I use int32 just to make sure no one at microsoft decides to change the size of int?

所以我在 c# 中读到 int 等于 int32 的地方。在 64 位机器上也是这样吗?我应该使用 int32 来确保微软没有人决定改变 int 的大小吗?

采纳答案by Robin Day

intis an alias for Int32

int是别名 Int32

longis an alias for Int64

long是别名 Int64

Their sizes will not change, at all, just use whichever one you need to use.

它们的大小根本不会改变,只需使用您需要使用的任何一个。

The use of them in your code is in no way related to 32bit and 64bit machines

在您的代码中使用它们与 32 位和 64 位机器没有任何关系

EDIT: In reference to the comments about Thread Safety. Here is a good question and answers that detail any issues you need to be aware of. Under C# is Int64 use on a 32 bit processor dangerous

编辑:参考关于线程安全的评论。这是一个很好的问题和答案,详细说明了您需要注意的任何问题。 在 C# 下,在 32 位处理器上使用 Int64 是危险的

回答by Michael Damatov

On both 32-bit and 64-bit machines:

在 32 位和 64 位机器上:

  • longis 64-bit, it's a synonym for System.Int64.
  • intis 32-bit, it's a synonym for System.Int32.
  • long是 64 位的,它是System.Int64.
  • int是 32 位的,它是System.Int32.

回答by this. __curious_geek

Simply NO. Types are consistent.

简单NO。类型一致。

回答by LukeH

You don't need to worry. An intis an Int32is a 32-bit signed integer, and that won't change, regardless of the platform that you're using.

你不必担心。An intis anInt32是一个 32 位有符号整数,无论​​您使用什么平台,它都不会改变。

See the Microsoft C# spec(section 1.3), the ECMA C# spec(section 8.2.1) and the ECMA CLI spec(section 8.2.2).

请参阅Microsoft C# 规范(第 1.3 节)、ECMA C# 规范(第 8.2.1 节)和ECMA CLI 规范(第 8.2.2 节)。

回答by zeocrash

Integer is 32 bit Long is 64 bit

整数是 32 位长是 64 位

On both 32 and 64 bit processors

在 32 位和 64 位处理器上

回答by zvolkov

I used Int32 in my first year with .NET (then 1.0). Mostly did it for cross-language readability, as Int32 looks the same in VB as in C#. Looking back, I see this entire concern was silly. Use native types and don't sweat it.

我在使用 .NET(然后是 1.0)的第一年就使用了 Int32。主要是为了跨语言可读性,因为 Int32 在 VB 中看起来与在 C# 中相同。回想起来,我发现这整个担忧是愚蠢的。使用本机类型,不要担心。