Take the int type as an example, specifying Convert.ToInt32 (object value), int. Parse (object value) and int. TryParse (string s,out int result) usage
I. Int.parse
Int. The underlying implementation principle of parse (can be ignored directly, without having to delve into it)
[SecuritySafeCritical]Internal Static unsafe intParseInt32 (strings, NumberStyles style, NumberFormatInfo info) { byte* Stackbuffer =stackalloc byte[0x72]; Numberbuffer Number=NewNumberbuffer (Stackbuffer); intnum =0; Stringtonumber (s, style,refNumber, info,false); if(Style & numberstyles.allowhexspecifier)! =numberstyles.none) {if(! HexNumberToInt32 (refNumberrefnum)) { Throw NewOverflowException (Environment.getresourcestring ("Overflow_int32")); } returnnum; } if(! NumberToInt32 (refNumberrefnum)) { Throw NewOverflowException (Environment.getresourcestring ("Overflow_int32")); } returnnum; }
Int. Parse () is a class-tolerant transformation that converts a string of numeric content into an int type.
Throws a ArgumentNullException exception if the string is empty;
Throws a FormatException exception if the string content is not a number;
Throws a OverflowException exception if the string content represents a number that is outside the range represented by the int type;
Two. Convert.ToInt32
The Convert.ToInt32 implementation is this way (the reflection source assembly is known):
Public Static int ToInt32 (string value) { ifnull) { return 0; } return int . Parse (value, CultureInfo.CurrentCulture); }
From the code above you can see that convert.toint32 can actually be seen as an int. Parse an improvement because it determines the case where the value equals null
Returns 0 when the Convert.ToInt32 parameter is null
However, when the Convert.ToInt32 parameter is String.Empty, the System.FormatException is thrown: the input string is malformed and the exception is incorrect.
Three. Int. TryParse
[SecuritySafeCritical]Internal Static unsafe BOOLTryParseInt32 (strings, NumberStyles style, NumberFormatInfo info, out intresult) { byte* Stackbuffer =stackalloc byte[0x72]; Numberbuffer Number=NewNumberbuffer (Stackbuffer); Result=0; if(! Trystringtonumber (s, style,refNumber, info,false)) { return false; } if(Style & numberstyles.allowhexspecifier)! =numberstyles.none) {if(! HexNumberToInt32 (refNumberrefresult)) { return false; } } Else if(! NumberToInt32 (refNumberrefresult)) { return false; } return true; }
Int. TryParse can be seen as a pair of int. The improvement of parse and Convert.ToInt32.
It also determines the case where the value is equal to NULL, and the null character String.Empty.
So it does not produce an exception, the conversion succeeds returns true, and the conversion fails to return false.
The last parameter is the output value, if the conversion fails, the output value is 0, if the conversion succeeds, the output value is the converted int value
Four. Comparison of Int.parse,convert.toint and Int.tryparse
1. Different parameters and applicable objects
Int. Parse parameter data type can only be string type, applicable object is string type of data
Convert.toint parameters are more, see the list of the most overloaded
Int. The TryParse parameter can only be a string type, and the applicable object is a string type of data
2. Unusual circumstances
The exception is mainly for cases where the data is null or ""
When the Convert.ToInt32 parameter is NULL, an exception is thrown when the 0;convert.toint32 parameter is returned as "";
Int. Throws an exception when the Parse parameter is null. ; Int. Throws an exception when the Parse parameter is "".
Int. TryParse
3. Different return values
Int. The difference between TryParse and Int.parse and Convert.toint in the return value is the return bool type. Gets the converted value obtained by the out result parameter.
Five. Attached
Convert.toint parameter Overloading list
|
name |
description |
|
ToInt32 (Boolean) |
Converts the specified Boolean value to an equivalent 32-bit signed integer. |
|
ToInt32 (Byte) |
Converts the value of the specified 8-bit unsigned integer to an equivalent 32-bit signed integer. |
|
ToInt32 (Char) |
Converts the value of the specified Unicode character to an equivalent 32-bit signed integer. |
|
ToInt32 (DateTime) |
Calling this method always throws InvalidCastException. |
|
ToInt32 (Decimal) |
Converts the value of the specified decimal number to an equivalent 32-bit signed integer. |
|
ToInt32 (Double) |
Converts the value of the specified double-precision floating-point number to an equivalent 32-bit signed integer. |
|
ToInt32 (Int16) |
Converts the value of the specified 16-bit signed integer to an equivalent 32-bit signed integer. |
|
ToInt32 (Int32) |
Returns the specified 32-bit signed integer, without performing the actual conversion. |
|
ToInt32 (Int64) |
Converts the value of the specified 64-bit signed integer to an equivalent 32-bit signed integer. |
|
ToInt32 (Object) |
Converts the value of the specified object to a 32-bit signed integer. |
|
ToInt32 (SByte) |
Converts the value of the specified 8-bit signed integer to an equivalent 32-bit signed integer. |
|
ToInt32 (Single) |
Converts the value of the specified single-precision floating-point number to an equivalent 32-bit signed integer. |
|
ToInt32 (String) |
Converts the specified string representation of a number to an equivalent 32-bit signed integer. |
|
ToInt32 (UInt16) |
Converts the value of the specified 16-bit unsigned integer to an equivalent 32-bit signed integer. |
|
ToInt32 (UInt32) |
Converts the value of the specified 32-bit unsigned integer to an equivalent 32-bit signed integer. |
|
ToInt32 (UInt64) |
Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer. |
|
ToInt32 (Object, IFormatProvider) |
Converts the value of the specified object to a 32-bit signed integer, using the specified culture-specific format information. |
|
ToInt32 (String, IFormatProvider) |
Converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information. |
|
ToInt32 (String, Int32) |
Converts the string representation of a number in a specified cardinality to an equivalent 32-bit signed integer. |
Three ways to convert data types Convert,parse and TryParse