I have a generic class X<T> ; This class has a covariant part, which I want to have access covariantly. Therefore, I define it on the interface IX<out T> . However, I want this interface to be visible only to the class itself, because it also contains methods that must be private .
Ie, inside the class itself, I can raise to IX<T> and use it covariantly. For instance:.
class X<T> : IX<T> { private interface IX<out T>{ // the private covariant interface void foo(); } // It grants access to the private method `foo` private T foo(){...} public T IX.foo(){ return foo(); } private static void someMethod(IX<T> x) { // Here I can use `x` covariantly } }
Is it possible? I had never heard of private nested interfaces before, since a private interface usually does not make any difference. However, with the help of generics, such an interface becomes necessary to implement "covariance only for private access."
When I try to compile this, I get the following error:
foo.cs(1,14): error CS0246: The type or namespace name `IX' could not be found. Are you missing an assembly reference? foo.cs(9,14): error CS0305: Using the generic type `X<T>.IX<S>' requires `1' type argument(s)
Which is understandable in principle, for the internal type of the generic type, a type parameter for the external type is required. Is there a way to execute this code correctly?
source share