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.
ToInt32 allows null value, it doesn't throw any errors Int. parse does not allow null value, and it throws an ArgumentNullException error.
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.
Parse(String) Converts the string representation of a number to its 32-bit signed integer equivalent.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With