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 given. Is there a reason, and should I care?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
They both declare 32 bit integers, and as other posters stated, which one you use is mostly a matter of syntactic style. However they don't always behave the same way. For instance, the C# compiler won't allow this:
but it will allow this:
Go figure.
There is no difference between
int
andInt32
, but asint
is a language keyword many people prefer it stylistically (just as withstring
vsString
).Once upon a time, the int datatype was pegged to the register size of the machine targeted by the compiler. So, for example, a compiler for a 16-bit system would use a 16-bit integer.
However, we thankfully don't see much 16-bit any more, and when 64-bit started to get popular people were more concerned with making it compatible with older software and 32-bit had been around so long that for most compilers an int is just assumed to be 32 bits.
int
is an alias forSystem.Int32
, as defined in this table: Built-In Types Table (C# Reference)I always use the aliased types (int, string, etc.) when defining a variable and use the real name when accessing a static method:
It just seems ugly to see something like int.TryParse(). There's no other reason I do this other than style.
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");