In C#, int
and Int32
are the same thing, but I\'ve read a number of times that int
is preferred over Int32
with no reason
You should not care in most programming languages, unless you need to write very specific mathematical functions, or code optimized for one specific architecture... Just make sure the size of the type is enough for you (use something bigger than an Int if you know you'll need more than 32-bits for example)
ECMA-334:2006 C# Language Specification (p18):
Each of the predefined types is shorthand for a system-provided type. For example, the keyword
int
refers to the structSystem.Int32
. As a matter of style, use of the keyword is favoured over use of the complete system type name.
There is no difference between int
and Int32
, but as int
is a language keyword many people prefer it stylistically (just as with string
vs String
).
As already stated, int
= Int32
. To be safe, be sure to always use int.MinValue
/int.MaxValue
when implementing anything that cares about the data type boundaries. Suppose .NET decided that int
would now be Int64
, your code would be less dependent on the bounds.
I always use the system types - e.g., Int32
instead of int
. I adopted this practice after reading Applied .NET Framework Programming - author Jeffrey Richter makes a good case for using the full type names. Here are the two points that stuck with me:
Type names can vary between .NET languages. For example, in C#, long
maps to System.Int64 while in C++ with managed extensions, long
maps to Int32. Since languages can be mixed-and-matched while using .NET, you can be sure that using the explicit class name will always be clearer, no matter the reader's preferred language.
Many framework methods have type names as part of their method names:
BinaryReader br = new BinaryReader( /* ... */ );
float val = br.ReadSingle(); // OK, but it looks a little odd...
Single val = br.ReadSingle(); // OK, and is easier to read
It makes no difference in practice and in time you will adopt your own convention. I tend to use the keyword when assigning a type, and the class version when using static methods and such:
int total = Int32.Parse("1009");