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?
Another question: can an int
hold a long
(by cast) without losing data on all platforms?
An int is a 32-bit integer; a long is a 64-bit integer. Which one to use depends on how large the numbers are that you expect to work with. int and long are primitive types, while Integer and Long are objects.
An “int” and a “long int” are the same thing in C++ in 32 bit and 64 bit systems. If you want a 64 bit integer, you want a “long long”.
int datatype is the most preferred type for numeric values. long datatype is less frequently used. It should only be used when the range of the numeric value is too high. It requires the most memory(8 bytes) in comparison to the other three data-types.
If you need to hold an integer larger than the Integer data type can hold, you can use the Long data type instead. Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With