如何创建一个可以同时包含堆栈和列表的引用,而无需在 C# 中进行强制转换?

How can I create a reference that can contain both a stack and a list without needing a cast in c#?

我希望能够引用超类中的集合,该集合需要在子类中包含堆栈或列表,但我无法理解如何实现它,任何想法?

类似于:

public class Group 
{
     Collection<Human> group;
}

public class PeopleStack : Group
{
     public PeopleStack()
     {
           this.group  = new Stack<Human>();   
     }
} 

public class Crowd : Group
{
     public Crowd()
     {
           this.group  = new List<Human>();   
     }
}     

堆栈定义为

public class Stack<T> : 
     System.Collections.Generic.IEnumerable<T>, 
     System.Collections.Generic.IReadOnlyCollection<T>, 
     System.Collections.ICollection

列表是

public class List<T> : 
     System.Collections.Generic.ICollection<T>, 
     System.Collections.Generic.IEnumerable<T>,
     System.Collections.Generic.IList<T>, 
     System.Collections.Generic.IReadOnlyCollection<T>, 
     System.Collections.Generic.IReadOnlyList<T>, 
     System.Collections.IList

因此您可以使用强(通用)类型之一 IEnumerable<Human>IReadOnlyCollection<Human> 来表示 group

当然应该是 protected 等,但我想你知道。