Should I use int or Int32
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?
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 struct System.Int32
. As a matter of style, use of the keyword is favoured over use of the complete system type name.
The two are indeed synonymous; int
will be a little more familiar looking, Int32
makes the 32-bitness more explicit to those reading your code. I would be inclined to use int
where I just need 'an integer', Int32
where the size is important (cryptographic code, structures) so future maintainers will know it's safe to enlarge an int
if appropriate, but should take care changing Int32
s in the same way.
The resulting code will be identical: the difference is purely one of readability or code appearance.
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:
public enum MyEnum : Int32
{
member1 = 0
}
but it will allow this:
public enum MyEnum : int
{
member1 = 0
}
Go figure.
链接地址: http://www.djcxy.com/p/474.html上一篇: sizeof(int)在x64上?
下一篇: 我应该使用int还是Int32