C# int、Int16、Int32 和 Int64 之间有什么区别?

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

What is the difference between int, Int16, Int32 and Int64?

c#.net

提问by Joby Kurian

What is the difference between int, System.Int16, System.Int32and System.Int64other than their sizes?

除了它们的大小之外intSystem.Int16System.Int32和之间有什么区别System.Int64

采纳答案by deanWombourne

Each type of integer has a different range of storage capacity

每种类型的整数都有不同的存储容量范围

   Type      Capacity

   Int16 -- (-32,768 to +32,767)

   Int32 -- (-2,147,483,648 to +2,147,483,647)

   Int64 -- (-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807)

As stated by James Sutherland in his answer:

正如 James Sutherland 在他的回答中所说:

intand Int32are 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 int where 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 Int32variables in the same way.

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

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

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

回答by duskwuff -inactive-

Nothing. The sole difference between the types istheir size (and, hence, the range of values they can represent).

没有。类型之间的唯一区别它们的大小(因此,它们可以表示的值范围)。

回答by JaredPar

The only real difference here is the size. All of the int types here are signed integer values which have varying sizes

这里唯一真正的区别是大小。这里的所有 int 类型都是有符号整数值,它们具有不同的大小

  • Int16: 2 bytes
  • Int32and int: 4 bytes
  • Int64: 8 bytes
  • Int16: 2 字节
  • Int32int:4个字节
  • Int64: 8 字节

There is one small difference between Int64and the rest. On a 32 bit platform assignments to an Int64storage location are not guaranteed to be atomic. It is guaranteed for all of the other types.

Int64和其余之间有一个小的区别。在 32 位平台Int64上,不能保证对存储位置的分配是原子的。它保证适用于所有其他类型。

回答by Sunil Kumar B M

  1. intand int32are one and the same (32-bit integer)
  2. int16is short int (2 bytes or 16-bits)
  3. int64is the long datatype (8 bytes or 64-bits)
  1. int并且int32是一且相同(32 位整数)
  2. int16是 short int(2 字节或 16 位)
  3. int64是 long 数据类型(8 字节或 64 位)

回答by deanWombourne

EDIT: This isn't quite true for C#, a tag I missed when I answered this question - if there is a more C# specific answer, please vote for that instead!

编辑:这对于 C# 来说并不完全正确,我在回答这个问题时错过了一个标签 - 如果有更具体的 C# 答案,请改为投票!



They all represent integer numbers of varying sizes.

它们都代表不同大小的整数。

However, there's a very very tiny difference.

然而,有一个非常非常微小的差异。

int16, int32 and int64 all have a fixedsize.

int16、int32 和 int64 都有固定的大小。

The size of an int depends on the architecture you are compiling for - the C spec only defines an int as larger or equal to a short though in practice it's the width of the processor you're targeting, which is probably32bit but you should know that it might not be.

int 的大小取决于您正在编译的体系结构 - C 规范仅将 int 定义为大于或等于 short,但实际上它是您所针对的处理器的宽度,可能是32 位,但您应该知道它可能不是。

回答by Mahesh

Int=Int32 --> Original long type

Int=Int32 --> 原始长型

Int16 --> Original int

Int16 --> 原始整数

Int64 --> New data type become available after 64 bit systems

Int64 --> 新数据类型在 64 位系统后可用

"int" is only available for backward compatibility. We should be really using new int types to make our programs more precise.

“int”仅用于向后兼容。我们应该真正使用新的 int 类型来使我们的程序更加精确。

---------------

---------------

One more thingI noticed along the way is there is no class named Intsimilar to Int16, Int32 and Int64. All the helpful functions like TryParsefor integer come from Int32.TryParse.

在此过程中我注意到的另一件事是没有Int类似于 Int16、Int32 和 Int64 的类。所有有用的函数(如TryParse整数)都来自Int32.TryParse.

回答by Praveen

According to Jeffrey Richter(one of the contributors of .NET framework development)'s book 'CLR via C#':

根据 Jeffrey Richter(.NET 框架开发的贡献者之一)的书“CLR via C#”:

int is a primitive type allowed by the C# compiler, whereas Int32 is the Framework Class Library type (available across languages that abide by CLS). In fact, int translates to Int32 during compilation.

int 是 C# 编译器允许的原始类型,而 Int32 是框架类库类型(适用于遵守 CLS 的语言)。实际上,int 在编译过程中会转换为 Int32。

Also,

还,

In C#, long maps to System.Int64, but in a different programming language, long could map to Int16 or Int32. In fact, C++/CLI does treat long as Int32.

In fact, most (.NET) languages won't even treat long as a keyword and won't compile code that uses it.

在 C# 中,long 映射到 System.Int64,但在不同的编程语言中,long 可以映射到 Int16 或 Int32。事实上,C++/CLI 确实将 long 视为 Int32。

事实上,大多数 (.NET) 语言甚至不会将 long 视为关键字,也不会编译使用它的代码。

I have seen this author, and many standard literature on .NET preferring FCL types(i.e., Int32) to the language-specific primitive types(i.e., int), mainly on such interoperability concerns.

我见过这位作者,以及许多关于 .NET 的标准文献更喜欢 FCL 类型(即 Int32)而不是语言特定的原始类型(即 int),主要是关于此类互操作性问题。

回答by tno2007

A very important note on the 16, 32 and 64 types:

关于 16、32 和 64 类型的一个非常重要的说明:

if you run this query... Array.IndexOf(new Int16[]{1,2,3}, 1)

如果您运行此查询... Array.IndexOf(new Int16[]{1,2,3}, 1)

you are suppose to get zero(0) because you are asking... is 1 within the array of 1, 2 or 3. if you get -1 as answer, it means 1 is not within the array of 1, 2 or 3.

你应该得到零(0),因为你在问......是1、2或3数组中的1。如果你得到-1作为答案,这意味着1不在1、2或3数组中.

Well check out what I found: All the following should give you 0 and not -1 (I've tested this in all framework versions 2.0, 3.0, 3.5, 4.0)

好吧,看看我发现了什么:以下所有内容都应该给你 0 而不是 -1(我已经在所有框架版本 2.0、3.0、3.5、4.0 中测试过)

C#:

C#:

Array.IndexOf(new Int16[]{1,2,3}, 1) = -1 (not correct)
Array.IndexOf(new Int32[]{1,2,3}, 1) = 0 (correct)
Array.IndexOf(new Int64[]{1,2,3}, 1) = 0 (correct)

VB.NET:

VB.NET:

Array.IndexOf(new Int16(){1,2,3}, 1) = -1 (not correct)
Array.IndexOf(new Int32(){1,2,3}, 1) = 0 (correct)
Array.IndexOf(new Int64(){1,2,3}, 1) = -1 (not correct)

So my point is, for Array.IndexOf comparisons, only trust Int32!

所以我的观点是,对于 Array.IndexOf 比较,只信任 Int32!

回答by Haris

int

整数

It is a primitive data type defined in C#.

它是在 C# 中定义的原始数据类型。

It is mapped to Int32 of FCL type.

它映射到 FCL 类型的 Int32。

It is a value type and represent System.Int32 struct.

它是一种值类型,表示 System.Int32 结构。

It is signed and takes 32 bits.

它是有符号的,占用 32 位。

It has minimum -2147483648 and maximum +2147483647 value.

它的最小值为 -2147483648,最大值为 +2147483647。

Int16

整数16

It is a FCL type.

它是整箱类型。

In C#, shortis mapped to Int16.

在 C# 中,short映射到 Int16。

It is a value type and represent System.Int16 struct.

它是一种值类型,表示 System.Int16 结构。

It is signed and takes 16 bits.

它是有符号的,占用 16 位。

It has minimum -32768 and maximum +32767 value.

它具有最小 -32768 和最大 +32767 值。

Int32

整数32

It is a FCL type.

它是整箱类型。

In C#, intis mapped to Int32.

在 C# 中,int被映射到 Int32。

It is a value type and represent System.Int32 struct.

它是一种值类型,表示 System.Int32 结构。

It is signed and takes 32 bits.

它是有符号的,占用 32 位。

It has minimum -2147483648 and maximum +2147483647 value.

它的最小值为 -2147483648,最大值为 +2147483647。

Int64

64位

It is a FCL type.

它是整箱类型。

In C#, longis mapped to Int64.

在 C# 中,long映射到 Int64。

It is a value type and represent System.Int64 struct.

它是一个值类型,代表 System.Int64 结构。

It is signed and takes 64 bits.

它是有符号的,占用 64 位。

It has minimum –9,223,372,036,854,775,808 and maximum 9,223,372,036,854,775,807 value.

它的最小值为 –9,223,372,036,854,775,808,最大值为 9,223,372,036,854,775,807。

回答by this.girish

They both are indeed synonymous, However i found the small difference between them,

它们确实是同义词,但是我发现它们之间的细微差别,

1)You cannot use Int32while creatingenum

1)Int32创建时不能使用enum

enum Test : Int32
{ XXX = 1   // gives you compilation error
}

enum Test : int
{ XXX = 1   // Works fine
}

2) Int32comes under System declaration. if you remove using.Systemyou will get compilation error but not in case for int

2)Int32属于系统声明。如果你删除using.System你会得到编译错误,但不是为了以防万一int