Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别

nt32. Parse (string)


Int32.Parse (string str) method converts the string representation of a number to its 32-bit signed integer equivalent. It takes a string and tries to extract an integer from it and returns the integer. When s is a null reference, it will throwArgumentNullException. If str is not an integer value, it will throw FormatException. When str represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example:

 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 

resultValue = Int32.Parse(str1); //-- 123 
resultValue = Int32.Parse(str2); //-- FormatException 
resultValue = Int32.Parse(str3); //-- OverflowException 
resultValue = Int32.Parse(str4); //-- ArgumentNullException 



Convert.ToInt32(string)


Convert.ToInt32(string str) method converts the specified string representation of 32-bit signed integer equivalent. Convert.ToInt32 underneath calls the Int32.Parse. The only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. If str is other than integer value, it will throw FormatException. When s represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example: 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 


resultValue = Int32.Parse(str1); //-- 123 
resultValue = Int32.Parse(str2); //-- FormatException 
resultValue = Int32.Parse(str3); //-- OverflowException 
resultValue = Int32.Parse(str4); //-- 0 


Int32.TryParse(string, out int)


This method is available in C# 2.0 and above. Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully else false. When input string   is a null reference, it will return 0 rather than throw ArgumentNullException as it was coming in above two methods. If input string  is other than an integer value, the out variable will have 0 rather than FormatException as it was coming in above two methods. When input string  represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowExceptionas it was coming in above two methods. For example

 

string str1 = "123"; 
string str2 = "123.45"; 
string str3 = "12312312356456456456456456456456"; 
string str4 = null; 


int resultValue; 
bool isParsed;
isParsed =Int32.TryParse(str1, out resultValue); //isParsed =>true; result => 123

isParsed =Int32.TryParse(str2, out resultValue); //isParsed => false; result => 0 
isParsed =Int32.TryParse(str3, out resultValue); //isParsed => false; result => 0 
isParsed =Int32.TryParse(str4, out resultValue); // isParsed => false; result => 0 


So from above you came to know about s several different ways to extract integers from strings in
.Net. You should therefore use the method that better suits your scenario. If you've got a string, and you expect it to always be an integer use Int32.Parse.If you are expecting input other than integer use Convert.ToInt32 and if you don’t want any exception you can go for Int32.TryParse


你可能感兴趣的:(convert)