(int)
和convert.toint()
方法有何区别?
像
object o = 123;
int i = (int)o;
and
int i = Convert.ToInt16(o);
两者有什么不同?
答案 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;
不会编译。