Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

int or Int32, long or Int64?

I'm modifiying an ASP.Net app in C# in which lots of the variables are delcared as Int32s or Int64s.  Lots of them are also declared as ints or longs.

I'm assuming there's no difference between these two that isn't hidden from me because there doesn't seem to be any bugs mixing and matching.  But its annoying me anyway.

My question is, which declarations shall I standardise on in this app, and why?  And why would someone do it the other way?

Thomas David Baker
Monday, July 21, 2003

In C#, "int" is an alias for Int32, and "long" is a synonym for "Int64". If you disassemble your code with ILDASM, you'll see that the underlying types are used.

You should standardize on one or the other. Neither is intrinsically superior, despite the massive religious debate you may have just spawned. :)

Brad Wilson (dotnetguy.techieswithcats.com)
Monday, July 21, 2003

I thought as much.  The Microsoft examples seem to use int and long and they are also more familiar to me so unless someone wants to give me a good reason I think I'll go with them.

Thomas David Baker
Monday, July 21, 2003

The language primitive type keywords refer to FCL types, obviously "int" for "System.Int32". Personally I prefer the FCL type names, it more closely matches many method names in the FCL and avoids confusion. Eg. "System.Convert.ToSingle" returns a "float"!!!

Pietro
Monday, July 21, 2003

Perhaps the beginnings of a good reason there.  Are the Convert static methods better/different from a cast?

Thomas David Baker
Monday, July 21, 2003

Ahhh!  But with int.Parse() or long.Parse I seem to have the exact same functionality for the "friendlier" words.  No?

Thomas David Baker
Tuesday, July 22, 2003

Something else to consider:  int does indeed map to Int32 in the current versions of the framework, but might it not map to an Int64 on a 64 bit version of the framework (which MS is currently working on)?

Fred Flintstone
Tuesday, July 22, 2003

You mean System.Int32.Parse(...)?
:-)

Pietro
Wednesday, July 23, 2003

On the 64 bit issue -- Microsoft is indeed working on a 64-bit version of the .NET Framework but I'm pretty sure int will NOT map to 64 bit on that system.  Reasons:

1. The C# ECMA standard specifically says that int is 32 bit and long is 64 bit.

2. Microsoft introduced additional properties & methods in Framework version 1.1 that return long values instead of int values, such as Array.GetLongLength in addition to Array.GetLength.

So I think it's safe to say that all built-in C# types will keep their current mapping.  As for the original question, always use int/Int32 as the default unless you're specifically targetting 64-bit machines; then use long/Int64.

Chris Nahr
Wednesday, July 23, 2003

The sizes of "short", "int", and "long" in C# are set in stone. It's not ambiguous like it was in C/C++.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, July 23, 2003

*  Recent Topics

*  Fog Creek Home