Converting to int16, int32, int64 - how do you know which one to choose?

Vidar picture Vidar · Nov 6, 2008 · Viewed 22.2k times · Source

I often have to convert a retreived value (usually as a string) - and then convert it to an int. But in C# (.Net) you have to choose either int16, int32 or int64 - how do you know which one to choose when you don't know how big your retrieved number will be?

Answer

Timothy Khouri picture Timothy Khouri · Nov 6, 2008

Everyone here who has mentioned that declaring an Int16 saves ram should get a downvote.

The answer to your question is to use the keyword "int" (or if you feel like it, use "Int32").

That gives you a range of up to 2.4 billion numbers... Also, 32bit processors will handle those ints better... also (and THE MOST IMPORTANT REASON) is that if you plan on using that int for almost any reason... it will likely need to be an "int" (Int32).

In the .Net framework, 99.999% of numeric fields (that are whole numbers) are "ints" (Int32).

Example: Array.Length, Process.ID, Windows.Width, Button.Height, etc, etc, etc 1 million times.

EDIT: I realize that my grumpiness is going to get me down-voted... but this is the right answer.