Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generics, interfaces and inheritance

Tags:

c#

generics

I've two interfaces:

public interface IAmA
{
}

public interface IAmB<T> where T : IAmA
{
}

And two classes implementing these interfaces like this:

public class ClassA : IAmA
{
}

public class ClassB : IAmB<ClassA>
{
}

When trying to use these classes as shown:

public class Foo
{
    public void Bar()
    {
        var list = new List<IAmB<IAmA>>();
        list.Add(new ClassB());
    }
}

I get this compiler error:

cannot convert from 'ClassB' to 'IAmB<IAmA>'

I know I can make the compiler happy using:

public class ClassB : IAmB<IAmA>
{
}

But I need to be able to be the Type parameter for IAmB<> in ClassB an implementation of IAmA.

like image 870
Alex Avatar asked Sep 10 '15 07:09

Alex


People also ask

What C is used for?

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 full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.

What is C in C language?

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.


1 Answers

The quick answer is that you can do what you ask by declaring the type parameter of IAmB<T> as covariant, only if the type is used as a return type:

public interface IAmB<out T> where T : IAmA
{
    T SomeMethod(string someparam);
}

out T means that you can use a more specific type than then one specified in the constraints.

You won't be able to use T as a parameter. The following won't compile:

public interface IAmB<out T> where T : IAmA
{
    void SomeMethod(T someparam);
}

From the documentation

You can use a covariant type parameter as the return value of a method that belongs to an interface, or as the return type of a delegate. You cannot use a covariant type parameter as a generic type constraint for interface methods.

This isn't a compiler quirk. Assuming you could declare a covariant method parameter, your list would end up containing some objects that couldn't handle an IAmB<IAmA> parameter - they would expect an input of ClassA or more specific. Your code would compile but fail at runtime.

Which begs the question - why do you want to use IAmB<ClassA> ?

You should think about before using this though, as there may be other, more suitable ways to address your actual problem. It's unusual to use a generic interface implementing a concrete type but trying to use it as if it were implementing another interface.

You can check the MSDN documentation's section on Covariance and Contravariance as well as Eric Lippert's an Jon Skeet's answers to this SO question: Difference between Covariance and Contravariance

like image 134
Panagiotis Kanavos Avatar answered Sep 21 '22 10:09

Panagiotis Kanavos