Wednesday, February 27, 2008

int vs Int32

int iViaKeyword = 0;
Int32 iViaClass = 0;

The above two lines of code declare the same exact Integer variable. Back in the VB6 days for example an Integer was 16 bit but in today's world in both VB.NET and C# the int Keyword represents a signed 32bit  integer.

So what is the difference, why have the two ways to declare the same exact thing? Lets examine both of our options in more detail to try and figure this out.

Comparing: Int32 to Int

Int32 is defined in the documentation as a Struct (System Namespace) that represents a signed 32bit integer.

Int instead is defined as a Keyword that points to the Int32 Struct and therefore also holds a signed 32bit integer.

Given this information both seem identical and can be used interchangeably. For example one could says iViaClass = iViaKeyword from our code above and get no errors nor a message that requests an explicit type conversion in C#. For all coding purposes these two are exactly the same.

To me personally this is a bit confusing as an implementation and I would have gotten ride of the int Keyword, but perhaps given the legacy comfort developers have typing int into their IDE this could perhaps explain its existence.

Int32, Optimized for the processor?

As for using Int32 vs. Int16 or vs. Int64 various documentation state that an Int32 is optimized by the compiler for the processor and therefore should be used in most cases where it wont overflow.

I do have to question this though now that 64 bit processors exist, does that mean that Int64 would be more optimized on a 64bit platform? I am not sure of this answer and will look into it later.

Coding for Change

I have seen some discussions in forums that argue when you type Int32 you guaranty that your code will target an Int32 no matter what, as this is a specific implementation. Using the Int keyword does not necessarily come with this guaranty as one day Microsoft might change the Int keyword to mean Int64 as it once changed Int from 16 to 32.

To me such a breaking change seems unlikely but one can never be 100% sure and I would give this argument some validity. If I was building a system that needed to stand the test of time I would consider following this advice.

Closing

I look forward to your feedback and any comments or corrections. Even this simple concept in .NET has a lot of depth and I look forward to any discussion that this might spawn.

2 comments:

Jon Skeet said...

Changing int in C# to mean anything other than 32 bits would be an horrific breaking change - it's never going to happen, any more than changing the default value is.

int and Int32 can be used *almost* (but not completely interchangably).

I always use int within code, but Int32 in method names - e.g.
int GetInt32 (...)
- so as to be language-agnostic.

Dmitry said...

Thanks Jon, that makes a lot of sense to me personally.

Perhaps this concern of Int changing is crazy but i decided to bring it up as people have discussed it on various forums postings.

I do like your idea of method naming with Int32, ill have to consider it for my own coding practices.