【问题标题】:C# How to iterate over a Dictionary of common parent class for generic children?C#如何遍历通用子类的公共父类字典?
【发布时间】:2019-03-18 04:56:39
【问题描述】:

我有这种情况:

class Animal 
{
  int size;
} 

class Dog : Animal 
{
 string Name;
}

class Cat : Animal 
{
 string Alias;
}

public void Check( Dictionary<string,Animal> animals ) 
{
 foreach(var pet in animals){...}
}

我的问题在这里:

如果狗扩展了动物,为什么字典不承认狗是动物?

您是否有允许传递子类字典并对其进行迭代的想法(不使用 (string,object) 的处理字典)?

public void MySituation()

    {
        Dictionary<string,Animal> dogs = new Dictionary<string,Dog>();
        ** ERROR 01**

        Dictionary<string,Cat> cats = new Dictionary<string,Cat>();
        Check(cats);
        ** ERROR 02**
    }

非常感谢。 编译器优化功能与您同在。

【问题讨论】:

    标签: c# covariance contravariance


    【解决方案1】:

    Dictionary&lt;string,Animal&gt; 的问题在于它允许您添加。如果您可以将“动物”添加到狗字典中,那么您很容易在其中找到一只猫,那么一切都会崩溃。所以编译器不会允许你将任何其他类型的字典视为相同。

    您需要做的是使用只读接口,例如IEnumerable

    public void Check( IEnumerable<KeyValuePair<string,Animal>> animals ) 
    {
         foreach(var pair in animals)
         {
             var animal = pair.Value;
             //Do something
         }
    }
    

    另一种选择是使您的方法通用:

    public void Check<T>( Dictionary<string,T>> animals ) where T : animal
    {
         foreach(T animal in animals)
         {
             //Do something
         }
    }
    

    如果您想了解这背后的理论,请参阅有关协变和逆变的答案之一,例如 this onethis one

    【讨论】:

    • 非常感谢,我需要知道这个概念的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多