C# inheritance in generics question

后端 未结 3 920
清歌不尽
清歌不尽 2021-01-18 03:14

I have two interfaces:

public interface A { 
 void aMethod(); 
}


public interface B : A {
 void bMethod();
} 

Later I\'m basically using

相关标签:
3条回答
  • 2021-01-18 03:36

    I don't think you're going to find a way around your problem because the thinking behind it seems flawed. To illustrate, let's create another interface:

    public interface C : A
    {
        void cMethod();
    }
    

    Now, let's use your code:

    Dictionary<int, A> dict = new Dictionary<int, B>();
    

    What happens when I try the following?

    C c = new ClassThatImplementsC(); 
    dict.Add(1, c);
    

    Take a look at Eric Lippert's Covariance and Contravarience FAQ for many, many more details.

    0 讨论(0)
  • 2021-01-18 03:45

    The feature you are looking for is what's referred to as generics variance (covariance and contravariance). There is limited support for this starting in .net framework 4. Here's an interesting post: How is Generic Covariance & Contra-variance Implemented in C# 4.0?

    And here's the MSDN entry on Covariance and Contravariance in Generics.

    0 讨论(0)
  • 2021-01-18 03:51

    Nope it's a covariance issue. If you could do:

    Dictionary<int, A> dict = new Dictionary<int, B>();
    

    It would be possible without a compiler error to put an object of Type A in dict.

    The problem is that dict looks like: Dictionary<int, A> but it is really type Dictionary<int, B>() (so placing an object of Type A would throw a runtime error because of an invalid cast), so you shouldn't be allowed to even try to place an object of Type A in dict, which is why you can't do :

    Dictionary<int, A> dict = new Dictionary<int, B>();
    

    It's protecting you from making a runtime mistake.
    You want to check out Eric Lippert's blog on the subject: http://blogs.msdn.com/b/ericlippert/

    It's one of his favorite topics to talk about, so it's quite thorough.

    0 讨论(0)
提交回复
热议问题