Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
Mike Avatar asked Apr 26 '12 15:04

Mike


People also ask

What is the difference between int32 Parse and int Parse?

ToInt32 allows null value, it doesn't throw any errors Int. parse does not allow null value, and it throws an ArgumentNullException error.

What is int32 Parse?

Parse(String) Method is used to convert the string representation of a number to its 32-bit signed integer equivalent. Syntax: public static int Parse (string str); Here, str is a string that contains a number to convert.

What does int Parse mean?

Parse(String) Converts the string representation of a number to its 32-bit signed integer equivalent.

Should I use Parse or convert?

If we know that we'll always get an integer as a string form, we can use the int. Parse() method. But if there is a chance that the string can be null or an object or other type, we should use the Convert.


2 Answers

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.

like image 196
Stilgar Avatar answered Oct 07 '22 18:10

Stilgar


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.

like image 44
Jon Avatar answered Oct 07 '22 17:10

Jon