Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between long and int in C#?

Tags:

c#

types

casting

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?

like image 565
Earlz Avatar asked Dec 16 '09 23:12

Earlz


People also ask

What is the difference between int and long?

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.

Is int same as long int?

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”.

Is long better than int?

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.

Is Long bigger than int?

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 .


1 Answers

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.

like image 195
thecoop Avatar answered Sep 20 '22 09:09

thecoop