你好
我想知道之间有什么不同:
Convert.ToInt16 or Convert.ToInt32 or Convert.ToInt64
Run Code Online (Sandbox Code Playgroud)
VS
Int.Parse
Run Code Online (Sandbox Code Playgroud)
他们俩都在做同样的事情所以只想知道有什么不同?
Hom*_*mam 10
Convert.ToInt将对象转换为整数,如果值为null,则返回0.
int x = Convert.ToInt32("43"); // x = 43;
int x = Convert.ToInt32(null); // x = 0;
int x = Convert.ToInt32("abc"); // throws FormatException
Run Code Online (Sandbox Code Playgroud)
Parse将字符串转换为整数,如果该值无法转换则抛出异常
int x = int.Parse("43"); // x = 43;
int x = int.Parse(null); // x throws an ArgumentNullException
int x = int.Parse("abc"); // throws an FormatException
Run Code Online (Sandbox Code Playgroud)