One, (int) variable name [coercion type conversion]:
This conversion is primarily used for numeric type conversions, from int to long,float,double,decimal types, and implicit conversions are possible, but the use of a long type to an int type requires an explicit conversion, which is how the data type is converted, or a compilation error occurs.
The method is unconditional for floating-point numbers and loses precision.
Of course, this method can also be used to convert an object to int, but the value of object is given a value of type int, otherwise a compilation error is generated, and an object is null when an error occurs.
The last thing to avoid is to handle the conversion of char type to int type, otherwise the value returned is ASCII code, not the value you want.
two, Int. Parse (String type variable name)
The method is to convert a string of numeric content to an int type, or to throw a ArgumentNullException exception if the string content is empty or null, or to throw a FormatException exception if the string content is not a number The OverflowException exception is thrown if the string content represents a number that is outside the range that the int type can represent.
One of the things you can do with this approach is to handle only string content , and the string content can only be within the range represented by the int type.
Third, Int. TryParse (string s, out int result)
This is also the way to convert a string of numeric content to an int type, but that way is more than int. The good thing about parse is that it does not appear to be abnormal. If the conversion succeeds returns True, False is returned if the conversion fails. Obviously, the last parameter is the output value, if the conversion fails, the output value is 0, and if the conversion succeeds, the corresponding value is output.
Iv. Convert.ToInt32
This method not only converts a string to an int type, but also converts the value of another type to an int type. If the variable is an object or string type, when its value is null, it returns 0, without causing a program error, but if the value of this string type is string. Empty, which can still cause a program error when transitioning to int. This method rounds the floating-point number.
This method, like coercion, cannot be used to handle char types, otherwise the ASCII code is returned.
C # (int)/int. Parse ()/int. The difference between TryParse ()/convent.toint32 ()--Recommended use of Int.tryparse ()