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
Difference between long and int in C#?
提问by Earlz
What is the actual difference between a long
and an int
in 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 int
hold a long
(by cast) without losing data on all platforms?
另一个问题:可以在不丢失所有平台上的数据的情况下(通过演员表)int
持有long
吗?
采纳答案by thecoop
an int
(aka System.Int32
within 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 long
with a value above Int32.MaxValue
or below Int32.MinValue
without losing data.
an int
(又名System.Int32
在运行时内)在任何平台上总是一个有符号的 32 位整数,a long
(又名System.Int64
)在任何平台上总是一个有符号的 64 位整数。因此,您不能在不丢失数据的情况下从long
高于Int32.MaxValue
或低于值的a进行转换Int32.MinValue
。
回答by Gonzalo
int
is 32 bits in .NET. long
is 64-bits. That is guaranteed. So, no, an int
can'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 int
is a System.Int32
and a long
is 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,如果您使用显式强制转换,则结果将出乎意料。