开发者

(int) and convert.toint() difference between them [duplicate]

开发者 https://www.devze.com 2023-03-14 04:11 出处:网络
This question already has answers here: Closed 11 years ago. Possible D开发者_StackOverflow中文版uplicate:
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消