Convert.ToInt16或32或64和Int.Parse有什么区别?

Sal*_*leh 8 c#

可能重复:
什么是int.Parse()和Convert.ToInt32之间的主要区别

你好

我想知道之间有什么不同:

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)