Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
String mystring = "1234 ";
Int Myint = 0;
Myint = convert. toint32 (mystring );
Console. Write (Myint + "\ r \ n ");
Myint = int32.parse (mystring );
Console. Write (Myint + "\ r \ n ");
Int32.tryparse (mystring, out Myint );
Console. Write (Myint + "\ r \ n ");
}
}
}
On the surface, it can be seen that the three methods have achieved the same effect!
In this caseCodeModify:
// String mystring = "1234 ";
String mystring = NULL;
Int Myint = 0;
Myint = convert. toint32 (mystring );
Console. Write (Myint + "\ r \ n ");
Myint = int32.parse (mystring );
Console. Write (Myint + "\ r \ n ");
Int32.tryparse (mystring, out Myint );
Console. Write (Myint + "\ r \ n ");
Running result:
If convert. toint32 () is null, 0 is returned instead of an exception;
Int32.parse () should throw an exception;
If int32.tryparse () does not throw an exception, true or false is returned to indicate whether the resolution is successful. If a parsing error occurs, the caller will get a value of 0.
Conclusion:
There are almost no differences between the three methods!
If you want to pursue perfection, consider the performance differences:
Int32.tryparse () is better than int32.parse () than convert. toint32 ().
Personal suggestion: Use int32.parse () in. net1.1; Use int32.tryparse () in. net2.0 ().
Why?
because: convert. toint32 will delegate the final parsing work to int32.parse, while int32.parse and int32.tryparse respectively proxy the parsing work directly to number. parseint32 and number. tryparseint32: the former throws an exception when a parsing error occurs, while the latter returns only false.