56

I know they are the same variable type, but is there an accepted standard 'style' for whether to use long or Int64?

I would like to use the most common one.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kyle
  • 28,411
  • 33
  • 120
  • 167
  • Check this out http://forums.asp.net/t/1278143.aspx/1 "Int64 = long Int32 = int Int16 = short" – SchautDollar Apr 22 '13 at 17:18
  • 4
    As always, this is a matter of preference. The important bit is to be consistent in what you use. That said, I believe most developers prefer `long` over `Int64`. – dtb Apr 22 '13 at 17:18
  • What do you want to happen when you recompile on a 128-bit machine? – Geoff Apr 22 '13 at 17:19
  • 3
    @Geoff first, it could be a century or two before anyone sees the need for a 128-bit machine, and second, .NET guarantees the widths of these datatypes, unlike C and C++ – jalf Apr 22 '13 at 17:19
  • 7
    I'm not sure I understand why I have been downvoted so harshly for this question. There are plenty of other stackoverflow questions asking what the proper style for something is. – Kyle Apr 22 '13 at 17:24
  • probably because there's a 100 questions on SO that discuss this exact thing – Filip Apr 22 '13 at 17:25
  • 1
    Also, its simply a Coke vs Pepsi question. All subjective. – Scott Adams Apr 22 '13 at 17:33
  • @ScottAdams also true – Filip Apr 22 '13 at 17:36
  • @fillip I find ones dicusing the difference between the two, but not which one is the better style. Care to link to a few? – Kyle Apr 22 '13 at 17:39
  • 3
    @Geoff: C# never went from 16 to 32 bit. And it is not VBA. It guarantees the precise size of these types (which may or may not come back to bite them at some point in the future) – jalf Apr 22 '13 at 18:00
  • Would this type of question be acceptable on Programmers Exchange? – CmdrTallen Sep 16 '14 at 14:12
  • 2
    I don't think this is a "non constructive" question as Visual Studio hints marks this issue as "IDE0049" "Name can be simplified" and the answer is valuable for those who want to know why. – JoeCool Jul 27 '19 at 11:03
  • 1
    @JoeCool I agree. Looking for the answer to that very Visual Studio hint is what led me here. From an embedded perspective, we often go the *other* way and specifically do typedefs for e.g. uint32_t so that we have these guarantees across platforms. So having a warning that to my ears sounds like "convert your precise definition into one that is defined for C# but has an ambiguous history in its parent languages" seems like an iffy choice - especially when dealing with e.g. cross-platform binary parsing, etc. – J Trana Oct 25 '20 at 02:06

6 Answers6

74

All documentation from Microsoft and 99.999% (or so) of the code you'll find online will use long. See for instance the numeric datatype definition in the C# reference: http://msdn.microsoft.com/en-us/library/exx3b86w.aspx

In the end, this is the same issue with using string or String. In general, lowercase names are used for value datatypes like numbers or characters, and uppercase names are used for classes. In C# Int64 is the complex datatype (a structure with fields, properties and methods, see the doc here) for the long value datatype (see the doc here). In general, people don't use the class Int64 for other than invoking methods from that class, such as Int64.Parse. So you will usually find something like this:

long variable = 9.5f;
string text = "8.4";
long anotherVariable = Int64.Parse(text);
Julián Urbano
  • 8,100
  • 1
  • 28
  • 51
16

You will get so many different opinions for a question like this since they're the exact same thing. So it's up to you. Microsoft pretty much always uses long, int and short in their documentation and examples. It's simply an Alias in VB.NET and C#. So I guess it's better usage to use them that way.

  • long instead of Int64
  • int instead of Int32
  • short instead of Int16
Aziz Shaikh
  • 15,104
  • 9
  • 55
  • 73
phadaphunk
  • 11,531
  • 14
  • 64
  • 104
5

C# uses these keywords like long, int string, as aliases for .NET-types.

  • int = System.Int32
  • long = System.Int64
  • string = System.String

The first argument to use these keywords is that it comes with the language, so use it when writing the language.

A second argument of using these keywords is that you do not have to put using System; above your codefile.

Another benefit could be that someone could create a new type called Int64 and put it somewhere in your namespace (I know... it would be crazy). If your older code uses the type Int64, your older code could stop functioning. If your older code was using long (an alias for System.Int64) than it would still work fine.

Martin Mulder
  • 11,592
  • 3
  • 19
  • 50
3

You should use long, int and short.

The one exception I know is in a function name; look at BitConverter.ToInt64.

The reason for that is that long is not defined in CIL while int64 is.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Peter
  • 33,550
  • 33
  • 138
  • 185
  • With respect to .NET framework types and naming methods, see the final paragraph of [this answer](http://stackoverflow.com/a/215422/1497596) to the question [What's the difference between String and string?](http://stackoverflow.com/q/7074/1497596). – DavidRR Apr 24 '14 at 15:46
1

I think it comes down to clarity versus compatibility. Int16, Int32 and Int64 are more clear than short, int and long. You know just by looking at Int16 that it is 16 bits in length.

DavidRR
  • 15,000
  • 17
  • 89
  • 169
Icemanind
  • 43,745
  • 45
  • 159
  • 272
  • 9
    Someone who doesn't know long is 64 bits has much bigger problems when reading code than this. – Filip Apr 22 '13 at 17:24
  • I agree with @Filip, if somebody doesn't know that short is 16 bit, int is 32 bit, and long is 64 bit is either incompetent or hasn't programmed in 20 years. – Pete Garafano Apr 22 '13 at 17:35
  • 6
    @TheGreatCO: That doesn't have anything to do with being incompetent or not having programmes in a while. C# hasn't been around for 20 years, and C/C++ have much different definitions for these data types. What Filip alludes to is that the equivalence between `long` and `Int64` *in C#* are basic principles *in C#*, so it can be expected that this a known fact -- or the programmer is at a very beginning stage of learning C#. – dtb Apr 22 '13 at 17:46
0

I think in most cases it will come down to personal preference, but if you are strictly talking "C#" without specifying the underlying platform, I'd think a bit differently. If you are developing to adhere to C# standards for portability across platforms and "future-proofing", "long" would be the best way to go as it is an actual construct in the language itself. "Int64" is an actual struct (System.Int64) in the framework.

Josh Johnson
  • 105
  • 2
  • 13