Http://www.cnblogs.com/legend_sun/archive/2008/12/23/1360533.html #
Convert. toint32, Int. parse (int32.parse), Int. tryparse, and (INT) can all be interpreted as converting data types to int. What are the differences between them?
Convert. toint32AndInt. parseSimilarly, convert. toint32 actually calls Int. parse:
- If the convert. toint32 parameter is null, 0 is returned;
- If the Int. parse parameter is null, an exception is thrown.
- If the convert. toint32 parameter is "", an exception is thrown;
- If the Int. parse parameter is "", an exception is thrown.
- Convert. toint32 can be converted to many types;
- Int. parse can only convert numeric strings.
Int. tryparseSimilar to int. parse, but it does not produce exceptions. If the conversion is successful, true is returned, and if the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.
Int m;
If (Int. tryparse ("2"), Out m)
{
...
}
Returns true, runs in {}, and assigns m a value of 2;
If (Int. tryparse ("DDD"), Out m)
{
...
}
Returns false. If {} is not run, M is assigned 0;
Use parse and tryparse to convert numeric strings
The basic numeric types include the parse and tryparse methods, which can be used to convert a numeric string to a specified numeric type. For example:
The tryparse method is introduced in. net2.0. it provides conditional parsing. This method returns a Boolean value indicating whether the parsing is successful, which saves the trouble of adding exception handling code. The following example uses the int32 type to demonstrate the two forms of tryparse:
In the second form, the first parameter is the text string to be parsed, and the second parameter is of the numberstyles Enumeration type, which indicates the possible content of the input string, the return value is returned in the fourth parameter.
Http://www.cnblogs.com/legend_sun/archive/2008/12/23/1360533.html #
Convert. toint32, Int. parse (int32.parse), Int. tryparse, and (INT) can all be interpreted as converting data types to int. What are the differences between them?
Convert. toint32AndInt. parseSimilarly, convert. toint32 actually calls Int. parse:
- If the convert. toint32 parameter is null, 0 is returned;
- If the Int. parse parameter is null, an exception is thrown.
- If the convert. toint32 parameter is "", an exception is thrown;
- If the Int. parse parameter is "", an exception is thrown.
- Convert. toint32 can be converted to many types;
- Int. parse can only convert numeric strings.
Int. tryparseSimilar to int. parse, but it does not produce exceptions. If the conversion is successful, true is returned, and if the conversion fails, false is returned. The last parameter is the output value. If the conversion fails, the output value is 0.
Int m;
If (Int. tryparse ("2"), Out m)
{
...
}
Returns true, runs in {}, and assigns m a value of 2;
If (Int. tryparse ("DDD"), Out m)
{
...
}
Returns false. If {} is not run, M is assigned 0;
Use parse and tryparse to convert numeric strings
The basic numeric types include the parse and tryparse methods, which can be used to convert a numeric string to a specified numeric type. For example:
The tryparse method is introduced in. net2.0. it provides conditional parsing. This method returns a Boolean value indicating whether the parsing is successful, which saves the trouble of adding exception handling code. The following example uses the int32 type to demonstrate the two forms of tryparse:
In the second form, the first parameter is the text string to be parsed, and the second parameter is of the numberstyles Enumeration type, which indicates the possible content of the input string, the return value is returned in the fourth parameter.