I read the C # document and said that the as
will not throw an runtime exception but will only return null.
Strangely, this code will even report an error directly at the compiler stage (because there is no common parent class) when its using (T)this
cast.
But why can we compile successfully by using as
in this case(even if the two types have no common parent class)?
...
return this as T;
it can run and the result is correct. .
return (T)this;
unable to compile (underline error CS0030)
. What's the matter? Is it true that as only checks whether the conversion is successful at runtime
I can understand that as does not throw exceptions when running, I can also understand that "two reference types without a common parent class" cannot be cast.
However, two classes that cannot be coerced can be compiled by using the as keyword instead. Why ?# (crazy sweat)
.
When the two types cannot be converted, why doesn't as
throw the same compile-time error as (T) this
?