C#中long和int的区别?

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

Difference between long and int in C#?

c#castingtypes

提问by Earlz

What is the actual difference between a longand an intin C#? I understand that in C/C++ long would be 64bit on some 64bit platforms(depending on OS of course) but in C# it's all running in the .NET runtime, so is there an actual distinction?

C# 中的along和 an之间的实际区别是什么int?我知道在 C/C++ 中,long 在某些 64 位平台上是 64 位(当然取决于操作系统),但在 C# 中它都在 .NET 运行时中运行,所以有实际区别吗?

Another question: can an inthold a long(by cast) without losing data on all platforms?

另一个问题:可以在不丢失所有平台上的数据的情况下(通过演员表)int持有long吗?

采纳答案by thecoop

an int(aka System.Int32within the runtime) is always a signed 32 bit integer on any platform, a long(aka System.Int64) is always a signed 64 bit integer on any platform. So you can't cast from a longwith a value above Int32.MaxValueor below Int32.MinValuewithout losing data.

an int(又名System.Int32在运行时内)在任何平台上总是一个有符号的 32 位整数,a long(又名System.Int64)在任何平台上总是一个有符号的 64 位整数。因此,您不能在不丢失数据的情况下从long高于Int32.MaxValue或低于值的a进行转换Int32.MinValue

回答by Gonzalo

intis 32 bits in .NET. longis 64-bits. That is guaranteed. So, no, an intcan't hold a long without losing data.

int在 .NET 中是 32 位。long是 64 位。这是有保证的。所以,不,int不能在不丢失数据的情况下保持很长时间。

There's a type whose size changes depending on the platform you're running on, which is IntPtr(and UIntPtr). This could be 32-bits or 64-bits.

有一种类型的大小取决于您运行的平台,即IntPtr(和 UIntPtr)。这可以是 32 位或 64 位。

回答by Walt W

Sure is a difference - In C#, a long is a 64 bit signed integer, an int is a 32 bit signed integer, and that's the way it always will always be.

当然是有区别的 - 在 C# 中,long 是一个 64 位有符号整数,一个 int 是一个 32 位有符号整数,而且它永远都是这样。

So in C#, a long can hold an int, but an int cannot hold a long.

所以在 C# 中,long 可以容纳 int,但 int 不能容纳 long。

C/C++ that question is platform dependent.

C/C++ 这个问题是平台相关的。

回答by Walt W

In C#, an intis a System.Int32and a longis a System.Int64; the former is 32-bits and the later 64-bits.

在C#中,anint是a System.Int32,along是a System.Int64;前者是 32 位,后者是 64 位。

C++ only provides vague guarantees about the size of int/long, in comparison (you can dig through the C++ standard for the exact, gory, details).

相比之下,C++ 只提供了关于 int/long 大小的模糊保证(您可以深入了解 C++ 标准以了解确切的、血腥的、细节)。

回答by Doug R

I think an int is a 32-bit integer, while a long is a 64-bit integer.

我认为 int 是一个 32 位整数,而 long 是一个 64 位整数。

回答by Anton Setiawan

int in C#=> System.Int32=>from -2,147,483,648 to 2,147,483,647.

C# 中的 int=> System.Int32=> 从 -2,147,483,648 到 2,147,483,647。

long in C#=> System.Int64 =>from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

C# 中的 long => System.Int64 => 从 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807

If your long data exceeds the range of int, and you use Convert.ToInt32 then it will throw OverflowException, if you use explicit cast then the result would be unexpected.

如果您的长数据超出了 int 的范围,而您使用 Convert.ToInt32 则它会抛出 OverflowException,如果您使用显式强制转换,则结果将出乎意料。