[英]Difference Between ToString() and Casting to String
string id = (string)result.Rows[0]["Id"];
Above line of code returns InvalidCastException
. 上面的代码行返回
InvalidCastException
。 Why it is that so? 为什么会这样呢?
But if I change the code to this 但是如果我将代码更改为此
string id = result.Rows[0]["Id"].ToString();
then it's working. 然后就可以了 Am I did anything wrong in my previous line of code?
我在上一行代码中做错了什么吗?
it's not working because ID
has a different Type. 它不起作用,因为
ID
的类型不同。 It's not string
- so you can convert it but not cast it. 它不是
string
-因此您可以将其转换但不能转换。
Let's have a look at different operations like if it's some dialog between you and compiler: 让我们看一下不同的操作,例如您和编译器之间是否存在一些对话框:
// here you say to compiler "hey i am 100% sure that it is possible
// to cast this `result.Rows[0]["Id]` to string
// this results in error if cast operation failed
string id = (string)result.Rows[0]["Id"];
// here you say to compiler: "please try to cast it to
// string but be careful as i am unsure that this is possible"
// this results in `null` if cast operation failed
string id = result.Rows[0]["Id"] as string;
// here you say to compiler: "please show me the string representation of
// this result.Rows[0]["Id"] or whatever it is"
// this results in invoking object.ToString() method if type of result.Rows[0]["Id"]
// does not override .ToString() method.
string id = result.Rows[0]["Id"].ToString();
I guess your rows' indexer's type isn't string
. 我猜你的行的索引器的类型不是
string
。 A cast looks like this: 演员表看起来像这样:
(TypeA)objB
This is only successful when 只有在以下情况下才能成功
objB
is of type TypeA
, objB
的类型为TypeA
,
objB
is of type TypeC
where TypeC
is a subclass of TypeA
, objB
的类型是TypeC
其中TypeC
是的一个子类TypeA
,
objB
is of type TypeC
where TypeC
is a superclass of TypeA
and objB's declaring type is TypeA
. objB
的类型是TypeC
其中TypeC
是一个超类TypeA
和objB的声明类型为TypeA
。
So, your code doesn't work. 因此,您的代码不起作用。
However , since every type derives from the holy Object
class, every type has a ToString
method. 但是 ,由于每种类型都源自神圣的
Object
类,因此每种类型都有一个ToString
方法。 Thus, whatever type Rows[0]["Id"]
returns, it has or has not a custom implementation of the ToString
method. 因此,无论
Rows[0]["Id"]
返回什么类型,它都具有或不具有ToString
方法的自定义实现。 The type of the return value of the ToString
method is always, you guessed it, String
. 您猜对了,
ToString
方法返回值的类型始终是String
。 So that's why ToString
works. 这就是
ToString
起作用的原因。
ToString()
does not simply cast your object, it calls its ToString
-method providing a "string-representation". ToString()
并不简单地转换您的对象,它调用其ToString
方法提供“字符串表示形式”。 Casting however means that the object itself IS a string and therefor you can cast it. 但是,强制转换意味着对象本身是字符串,因此您可以强制转换。
Also have a look here: Casting to string versus calling ToString 也可以在这里看看: 转换为字符串与调用ToString
EDIT: The ToString
-method derived from object
can be used to give a representation of any arbitrary object. 编辑:从
object
派生的ToString
方法可用于提供任何任意对象的表示形式。
MyClass
{
int myInt = 3;
public override string ToString() {
return Convert.ToString(myInt);
}
}
If ToString
is not overridden within your class, than it´s default return-value is the class´ typename. 如果您的类中没有重写
ToString
则默认的返回值是类的类型名。
使用ToString(),您可以将row0的ID转换为字符串,但在其他情况下,则将其转换为字符串,这在当前情况下是不可能的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.