I have a generic class X<T>
; This class has a covariant part that I want to be able to access covariantly. So I factor it out into an interface IX<out T>
. However, I want this interface to be visible only to the class itself, because it contains also methods that are ment to be private
.
I.e., inside the class itself, I can upcast to IX<T>
and use it covariantly. E.g.:
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 this possible? I have never heard of private
nested interfaces before, since a private interface usually makes no sence at all. However, with generics such an interface becomes necessary for implementing "private-only covariance".
When I try to compile this, I receive 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 basically clear, an inner type of a generic type needs a type parameter for the outer type. Is there a way to get this code to compile correctly?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Edit: it looks like this compiles on the Roslyn / C# 6 tech preview, but does not compile on the MS C# 5 compiler or the mono compiler.
Yes, like this - but note that actually the inner T
is unnecessary in many ways, and if you retain it - it would be useful to name it TInner
or something to avoid confusion, since the T
in X<T>
is technically a different thing than X<>.IX<T>
, even though they will always be the same actual type in practice:
class X<T> : X<T>.IX<T>
{
private interface IX<out TInner>
{ // the private covariant interface
void foo();
}
// It grants access to the private method `foo`
private T foo() { throw new NotImplementedException(); }
void X<T>.IX<T>.foo() { throw new NotImplementedException(); }
private static void someMethod(IX<T> x)
{
// Here I can use `x` covariantly
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With