Given these types:
class A { }
class B
{
public static implicit operator A(B me)
{
return new A();
}
}
class Test<T> where T : A { }
I tried
var b = new Test<B>();
And expected it to fail, which it did. But the error message is
The type ‘B’ cannot be used as type parameter ‘T’ in the generic type or method ‘Test’. There is no implicit reference conversion from ‘B’ to ‘A’.
But there is an implicit reference conversion from B to A. Is this just a strange message? There is not an implicit reference conversion as Adam Robinson’s answer shows. The message is correct.
Note that MSDN says:
where T : (base class name) – The type argument must be or derive from the specified base class.
Which explains why it is not allowed since B does not derive from A
No, what you’re trying to do is not possible. An implicit reference conversion is not the same thing as an implicit type conversion. Your code defines an implicit type conversion, which is where you could do the following:
Note, however, that
fooandbarnow contain different references. The implicit type conversion creates a new instance ofAthat should be (by convention) logically equivalent tofoo. But the point is that it’s a different reference.A reference conversion would be where the reference itself does not change, which means that the type in question must either inherit from (for classes) or implement (for interfaces) the type in question. If I do this:
Then my code above will now hold the same reference in
fooandbar. This is what is meant by an implicit reference conversion. Conversely, an explicit reference conversion would be downcasting, like this:Again, the references are the same, but the cast was explicit.
So, in short, the MSDN documentation is clearer but less precise.