Written by someone elseArticleSimplified, the original here: http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx
Few words.Code:
Code
Private List < Int32 > Testparse (string [] strings)
{
Int32 intvalue;
List<Int32>Intlist= NewList<Int>();
For(IntI= 0; I< 5000000; I++)
{
Intlist. Clear ();
Foreach(String StrInStrings)
{
Try
{
Intvalue=Int32.parse (STR );
Intlist. Add (intvalue );
}
Catch(System. argumentexception)
{}
Catch(System. formatexception)
{}
Catch(System. overflowexception)
{}
}
}
ReturnIntlist;
}
PrivateList<Int32>Testtryparse (string [] strings)
{
Int32 intvalue;
List<Int32>Intlist= NewList<Int>();
Boolean ret;
For(IntI= 0; I< 5000000; I++)
{
Intlist. Clear ();
Foreach(String StrInStrings)
{
RET=Int32.tryparse (STR,OutIntvalue );
If(RET)
{
Intlist. Add (intvalue );
}
}
}
ReturnIntlist;
}
PrivateList<Int32>Testconvert (string [] strings)
{
Int32 intvalue;
List<Int32>Intlist= NewList<Int>();
For(IntI= 0; I< 5000000; I++)
{
Intlist. Clear ();
Foreach(String StrInStrings)
{
Try
{
Intvalue=Convert. toint32 (STR );
Intlist. Add (intvalue );
}
Catch(System. formatexception)
{}
Catch(System. overflowexception)
{}
}
}
ReturnIntlist;
}
Test the correct data:
{"123", "4567", "7890", "1", "1231280", "10 "}.
The performance of the three methods is similar.
Test Error Data:
{"12345", null, "123", "1324dfs", "51235 ",String. Empty, "43", "4123412341234123412341234123412341234123 "}.
What you can see: tryparse> convert> parse
The reason is that convert and parse need to handle exceptions, especially the argumentnullexception processing overhead of parse.
Summary:
It is strongly recommended to use tryparse without special requirements.