5

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();
    }
}
verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • 2
    Bet `(D)(object)t` works – Ben Voigt May 25 '12 at 00:28
  • possible duplicate of [Value of type 'T' cannot be converted to](http://stackoverflow.com/questions/4092393/value-of-type-t-cannot-be-converted-to) – Kirk Woll May 25 '12 at 00:32
  • 1
    From a comment in [this link](http://stackoverflow.com/questions/1613314/generic-type-casting-method-net) I found a link to [your answer](http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx). Essentially, there are some variable types that cannot be casted to other types (more specifically there are rules about casting boxed types). Since the compiler has no idea about what T is at compile time it has to play it safe and deny the cast. – Mark M May 25 '12 at 00:39
  • @BenVoigt It does, but I don't know why the `(object)` is necessary... – verdesmarald May 25 '12 at 01:09
  • @KirkWoll The accepted answer to that question is "[T]he compiler doesn't know that T is string. Therefore, it doesn't let you cast. [...] You need to cast to object, (which any T can cast to), and from there to string". How is that any different? The compiler doesn't know that some arbitrary `object` is `string` either... In fact it seems to me that "the compiler can't guarantee X is Y" is the whole point of explicit casts in the first place! – verdesmarald May 25 '12 at 01:23
  • @veredesmarald, it's important to remember that `T` is *not* the same thing as `object`. It is a type parameter representing a concrete type. Consider if `T` is actually `int`; is this cast legal: `(string)5`? What if `T` is actually `Thread`? Is `(string)new Thread()` legal? The answer is no, whereas `(string)(object)5` is perfectly legal (inasmuch as the error happens at runtime, not compile-time), and analogous to what SLaks is demonstrating. – Kirk Woll May 25 '12 at 03:27
  • @KirkWoll I understand the compiler flagging things like `(string)5` because these casts will *always* fail at runtime. It seems the question the compiler asks when analysing `(X)y` is "is `X` a sub/supertype of `typeof(y)`?" In the case where `y` is generic, I thought the answer would be "Uh, maybe? let the runtime work it out." but from what you have said above it is just "No" and the cast is flagged at compile time. Is this the right way to look at it? – verdesmarald May 25 '12 at 04:26
  • Generics is a *compile-time* feature (albeit with excellent runtime support). Everything has to be worked out statically. (so yes, that's the right way to look at it) – Kirk Woll May 25 '12 at 04:28
  • @MarkM Yet we know `T` is not boxed: `where T : B` and `B` is a class. – Branko Dimitrijevic May 25 '12 at 18:23

3 Answers3

3

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)
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
  • So, why does `as` compile? What's the difference between cast and `as` in this case (_beside_ its run-time behavior)? – Branko Dimitrijevic May 25 '12 at 09:14
  • `as` gives the same error in the same situation. EDIT: actually, in the OP's example it does not. Will have to play with it some more. – Dave Cousineau May 25 '12 at 15:30
  • `as` is an entirely different animal. See [this msdn link](http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.80%29.aspx) - it is equivalent to `expression is type ? (type)expression : (type)null` – Mark M May 26 '12 at 06:42
1

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

TGH
  • 38,769
  • 12
  • 102
  • 135
0

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?

JotaBe
  • 38,030
  • 8
  • 98
  • 117