Possible D开发者_StackOverflow中文版uplicate:
difference between Convert.ToInt32 and (int)
What it difference between (int)
and convert.toint()
methods?
like
object o = 123;
int i = (int)o;
and
int i = Convert.ToInt16(o);
What is the difference in both of them?
The first method is (Un)Boxing the second method is conversion
You can convert another type to an int but to cast to an int it needs to be a boxed int.
e.g.:
string s = "123";
int i = Convert.ToInt16(s);
or
object o = 123;
int i = (int)o;
works fine. However:
string s = "123";
int i = (int)s;
won't compile.
精彩评论