This is a very good question. I found that you can make the error go away by specifying A<T>.B<T1>
:
public static implicit operator bool(A<T>.B<T1> b) => true;
So then I started to wonder why in this particular instance you need to qualify the inner class, because normally you don't.
Essentially, what you have written is an implicit conversion that can accept a type other than the enclosing type. Note that A<int>.B<string>
and A<string>.B<string>
are different classes.
Let's use a normal method, instead of an implicit conversion, to illustrate what's happening more clearly.
public class A<T>
{
public class B<T1>
{
public static void F(B<T1> i) {}
}
}
Note the absence of the inheritance clause. Bear with me for now. Here B<T1>
actually means A<T>.B<T1>
. This means that we can't do something like this:
A<int>.B<string>.F(new A<string>.B<string>()); // cannot convert type ...
So it would seem just writing B<T1>
in the conversion would work. But when you introduce the inheritance clause...
public class A<T>
{
public class B<T1>: A<T1>
{
public static void F(B<T1> i) {}
}
}
A<int>.B<string>.F(new A<string>.B<string>()); // suddenly this compiles
This means that you can now pass something else other than A<T>.B<T1>
to the implicit conversion, and that is not allowed.
public static implicit operator bool(B<T> b)
, notpublic static implicit operator bool(A<T> b)
– Azerbaijanibool(A<T> b)
. It should probably bebool(B<T> b)
. – PrudentialB<T>
instead of anA<T>
, so the two code snippets aren't really comparable. If you change the parameter type toA<T>
then it will also produce an error. – Agee