System.Convert.ToInt vs (int)

前端 未结 4 1657
猫巷女王i
猫巷女王i 2020-12-18 22:38

I noticed in another post, someone had done something like:

double d = 3.1415;
int i = Convert.ToInt32(Math.Floor(d));

Why did they use the

4条回答
  •  情深已故
    2020-12-18 23:28

    Convert.ToInt32() is used on strings (http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx) while casting can only be used on types that have internal converters (numeric types). The real trick comes in deciding between Int32.Parse and Convert.ToInt32(). Convert.ToInt32() is tolerant of a null parameter and returns 0 while Int32.Parse() will throw an ArgumentNullException.

提交回复
热议问题