When I try to use a user-defined cast operator from an interface type to a generic struct type, I get a compile error stating the type can't be converted:
public interface J { }
public struct S<T> {
public static explicit operator S<T>(T value) {
return new S<T>();
}
}
public static class C {
public static S<J> Test(J j) {
return (S<J>)j; // <- error: cannot convert type 'J' to type 'S<J>'
}
}
Note that if J were a class, the conversion would work.
There is a similar question about converting to a class, with the answer being that a derived class might implement the interface and create ambiguity over whether the user-defined cast should be used. But for structs there can be no derived type, and the compiler knows that my struct does not implement J.
Perhaps it's to avoid unexpected semantic changes as interfaces implementations new interfaces? Maybe it's just accidental? Maybe I'm making a naive mistake? A quote from the spec would be great, but really I'd like the reason it was designed that way in the first place.