Difference between long and int in C#?

74,202

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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

Share:
74,202

Related videos on Youtube

Earlz
Author by

Earlz

Hello there! My name's Jordan Earls, but most people online know me as "earlz". I'm the lead developer and a co-founder of the Qtum project which brings the Ethereum Virtual Machine (ie, the thing that makes Solidity contracts function) to a UTXO based blockchain similar to Bitcoin. I've been programming since I was 13 and am completely self-taught. Low-level code like assembly and pointer arithmetic is the fun stuff for me. I also make music when I have time even though it's usually awful. Most of my personal projects are open source and BSD licensed. The majority of them are at bitbucket with the rest of them being listed on github Also, you can follow me on the twitters @earlzdotnet

Updated on September 30, 2021

Comments

  • Earlz
    Earlz over 2 years

    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?

    • Yuriy Faktorovich
      Yuriy Faktorovich over 14 years
      In addition to the answers, note that long and int have static MaxValue and MinValue constants.
    • Earlz
      Earlz over 14 years
      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..
    • thecoop
      thecoop over 14 years
      The platform is abstracted away in .NET, so you always get a consistent runtime (for managed code, at least)
    • Joel Coehoorn
      Joel Coehoorn over 14 years
      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.
    • Eric Lippert
      Eric Lippert over 14 years
      The difference of a long and an int is a long, of course. So is the sum. :-)
  • Vetras
    Vetras over 8 years
    ok. but then why does this page msdn.microsoft.com/en-us/library/296az74e.aspx shows the same number from MAX INT and MAX LONG ?? an error on the microsoft page?
  • thecoop
    thecoop over 8 years
    Those are C++ types, not C#
  • Admin
    Admin almost 7 years
    The first page when you google C# long max value is this page - msdn.microsoft.com/en-us/library/296az74e.aspx - which is NOT for C#. That seems why the OP asked the question.