49

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?

Earlz
  • 62,085
  • 98
  • 303
  • 499
  • 1
    In addition to the answers, note that long and int have static MaxValue and MinValue constants. – Yuriy Faktorovich Dec 16 '09 at 23:11
  • 1
    I knew this but I wasn't for sure if these changed depending on 32 bit and 64 bit platforms.. Searching on google yielded just a lot of C/C++ results.. – Earlz Dec 16 '09 at 23:14
  • The platform is abstracted away in .NET, so you always get a consistent runtime (for managed code, at least) – thecoop Dec 16 '09 at 23:17
  • Don't think of the .net CLR as virtual machine. Think of it as a just-in-time compiler and this makes more sense. .Net code is compiled to fully-native machine code at app startup before anything runs. – Joel Coehoorn Dec 16 '09 at 23:31
  • 7
    The difference of a long and an int is a long, of course. So is the sum. :-) – Eric Lippert Dec 17 '09 at 00:23

6 Answers6

75

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
thecoop
  • 45,220
  • 19
  • 132
  • 189
  • 5
    ok. but then why does this page https://msdn.microsoft.com/en-us/library/296az74e.aspx shows the same number from MAX INT and MAX LONG ?? an error on the microsoft page? – Vetras Jan 22 '16 at 16:04
  • 5
    Those are C++ types, not C# – thecoop Jan 22 '16 at 16:33
  • 5
    The first page when you google `C# long max value` is this page - https://msdn.microsoft.com/en-us/library/296az74e.aspx - which is NOT for C#. That seems why the OP asked the question. –  Jul 27 '17 at 03:56
28

int in C#=> System.Int32=>from -2,147,483,648 to 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

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.

Anton Setiawan
  • 913
  • 10
  • 15
8

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.

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.

Gonzalo
  • 20,805
  • 3
  • 75
  • 78
5

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.

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

C/C++ that question is platform dependent.

Walt W
  • 3,261
  • 3
  • 30
  • 37
4

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++ only provides vague guarantees about the size of int/long, in comparison (you can dig through the C++ standard for the exact, gory, details).

0

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

Doug R
  • 5,749
  • 2
  • 28
  • 32