Reputation: 11866
I stumbled across this odd case yesterday, where t as D
returns a non-null value, but (D)t
causes a compiler error.
Since I was in a hurry I just used t as D
and carried on, but I am curious about why the cast is invalid, as t
really is a D
. Can anyone shed some light on why the compiler doesn't like the cast?
class Program
{
public class B<T> where T : B<T> { }
public class D : B<D> { public void M() { Console.Out.WriteLine("D.M called."); } }
static void Main() { M(new D()); }
public static void M<T>(T t) where T : B<T>
{
// Works as expected: prints "D.M called."
var d = t as D;
if (d != null)
d.M();
// Compiler error: "Cannot cast expression of type 'T' to type 'D'."
// even though t really is a D!
if (t is D)
((D)t).M();
}
}
EDIT: Playing around, I think this is a clearer example. In both cases t
is constrained to be a B
and is maybe a D
. But the case with the generic won't compile. Does the C# just ignore the generic constraint when determining if the cast is legal? Even if it does ignore it, t
could still be a D
; so why is this a compile time error instead of a runtime exception?
class Program2
{
public class B { }
public class D : B { public void M() { } }
static void Main()
{
M(new D());
}
public static void M(B t)
{
// Works fine!
if (t is D)
((D)t).M();
}
public static void M<T>(T t) where T : B
{
// Compile error!
if (t is D)
((D)t).M();
}
}
Upvotes: 5
Views: 460
Reputation: 13158
In your second example you can change
((D)t).M();
to
((D)((B)t)).M();
You can cast from B
to D
, but you can't necessarily cast from "something that is a B
" to D
. "Something that is a B
" could be an A
, for example, if A : B
.
The compiler error comes about when you are potentially jumping from child to child in the hierarchy. You can cast up and down the hierarchy, but you can't cast across it.
((D)t).M(); // potentially across, if t is an A
((D)((B)t)).M(); // first up the hierarchy, then back down
Notice also that you might still get a runtime error with ((D)((B)t)).M();
if t
is not actually a D
. (your is
check should prevent this though.)
(Also notice that in neither case is the compiler taking into account the if (t is D)
check.)
A last example:
class Base { }
class A : Base { }
class C : Base { }
...
A a = new A();
C c1 = (C)a; // compiler error
C c2 = (C)((Base)a); // no compiler error, but a runtime error (and a resharper warning)
// the same is true for 'as'
C c3 = a as C; // compiler error
C c4 = (a as Base) as C; // no compiler error, but always evaluates to null (and a resharper warning)
Upvotes: 3
Reputation: 39248
Change it to
public static void M<T>(T t) where T : D
This is the appropriate restriction if you want to mandate that T has to be of type D.
It won't compile if your restriction defines T as D of T.
Ex: You can't cast List<int> to int
or vice versa
Upvotes: 1
Reputation: 39025
Your template function have a constraint that requires T
to be B<T>
.
So, when your compiler tries to convert the object t
of type T
to D
it can't do it. Because T
is guranteed to be B<T>
, but not D
.
If you add a constraint to require that T
is D
, this will work. i.e. where T: B<T>, D
or simply where T: D
which also guarantees that T
is B<T>
, beacuse of the inheritance chain.
And the second part of the question: when you call t as D
it's checked at runtime. And, at runtime, using polymorphism, it's verified that t
can be converted to D
type, and it's done without errors.
NOTE: by the way, this code is sooooo strange. Are you sure about what you're doing?
Upvotes: 0