(int)和convert.toint()之间的区别

时间:2011-06-21 10:24:10

标签: c# .net

  

可能重复:
  difference between Convert.ToInt32 and (int)

(int)convert.toint()方法有何区别?

object o = 123;
int i = (int)o;
and
int i = Convert.ToInt16(o);

两者有什么不同?

2 个答案:

答案 0 :(得分:1)

第一种方法是(Un)Boxing,第二种方法是conversion

答案 1 :(得分:0)

你可以将另一种类型转换为int,但要转换为int,它需要是一个盒装的int。

e.g:

string s = "123";
int i = Convert.ToInt16(s);

object o = 123;
int i = (int)o;

工作正常。但是:

string s = "123";
int i = (int)s;

不会编译。

相关问题