I guess your rows' indexer's type isn't string
. A cast looks like this:
(TypeA)objB
This is only successful when
objB
is of type TypeA
,
objB
is of type TypeC
where TypeC
is a subclass of TypeA
,
objB
is of type TypeC
where TypeC
is a superclass of TypeA
and objB's declaring type is TypeA
.
So, your code doesn't work.
However, since every type derives from the holy Object
class, every type has a ToString
method. Thus, whatever type Rows[0]["Id"]
returns, it has or has not a custom implementation of the ToString
method. The type of the return value of the ToString
method is always, you guessed it, String
. So that's why ToString
works.