Mike
Mike

Reputation: 2547

Int32.Parse vs int.Parse

It is a common practice to use C# type aliases instead of CTS System.* types (int instead of Int32 and string instead of String). However it's not clear to me what to use to call a static method of a type in this case: an alias or a system type.

Microsoft does not seem to define any guidelies to use aliases instead of System types. But in MSDN aliases are used for variables and CTS equivalents are used for static calls. For example MSDN: Parsing Numeric Strings

int number;
Int32.TryParse(value, out number);

StyleCop defines the contrary in SA1121 - to always use aliases. So int.Parse is ok while Int32.Parse is not.

This question is a matter of style (in my opinion). But I don't understand reasons to use CTS type for static calls.

Upvotes: 13

Views: 9711

Answers (3)

Tigran
Tigran

Reputation: 62248

I strongly suggest to use int.Parse(...) instead of int32.If you immagine someone read your code running it on 64 bit machine, and it's probbale that he is not aware of that int32 and int are just aliases, it will make him to do a wrong assumptions.

In other words, even if from functional point of view there is no any difference, to avoid ambiguity in the code (referencies to presumably 32 bit code) I would suggest to use int.Parse(..)

Upvotes: 1

Stilgar
Stilgar

Reputation: 23551

I personally always use full class names for static method calls. This underlines the fact that they are in fact classes that contain pieces of code instead of simplest possible (primitive) data which the aliases imply.

I always use aliases for variables.

Upvotes: 9

Jon
Jon

Reputation: 437336

There is absolutely no technical difference, just coding style. Personally I advocate int.Parse, but there are many well-known experts that prefer the opposite.

Upvotes: 6

Related Questions