开发者

Difference between typecasting and parsing?

What is the big difference between parsing and typecasting? I try to use type casting to a string and it gives me error.

Something like this:

开发者_如何转开发
string str = "10";
int i = (int) str;


For type casting to work the types need to be compatible:

object str = 10;
int i = (int) str;

Parsing is conversion between different types:

string str = "10";
int i = int.Parse(str);


Casting works when the objects share some piece of inheritance. But in your case

int i = (int) str;

You are dealing with implicit automatic conversion. In which the compiler will automatically widden/losen a .NET built-in type. For a complete guide go here and look for Converting and Casting

Int32.Parse(...

Parsing is for when they are two unrelated objects, but there is a way of converting one way to another.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜