开发者

Casting an Object type to DataRow & IDatareader

Casting an Object 开发者_JAVA技巧type to DataRow & IDatareader


You cant just cast an object to either of those you mentioned. They have to be either of those or derive from those types.

There is no alchemy in programming.


Casting can only happen between compatible types. To transform two incompatible types you need to perform conversion.

Ex: You cannot cast a string into int and vice versa since they are incompatible types so there is a way of converting both into one another.

int x = 5;
string str = string.Empty;    

str = x.ToString(); //Convert int x into string str.
x = Convert.ToInt32(str);  //Convert string str into int x.

Instead of writing the whole story explaining difference between casting and conversion here, I'd rather recommend you to read this interesting conversation on SO. Do read Eric Lippert's answer. Eric Lippert works with Microsoft in C# compiler development team.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜