サブクラスが追加された基本タイプのコレクションを持つことができます。たとえば、次のように機能します。
// Using:
public class Foo {} // Base class
public class Bar : Foo {} // Subclass
// Code:
List<Foo> list = new List<Foo>();
HashSet<Foo> hash = new HashSet<Foo>();
list.Add(new Bar());
list.Add(new Foo());
hash.Add(new Bar());
「Bar」は「Foo」の特定のタイプであるため、Fooのコレクションに追加することは完全に合法です。
ただし、.NET 4と共分散のout修飾子が必要になるまで、次のことはできません。
IEnumerable<Foo> list = new List<Bar>(); // This isn't supported in .NET 3.5...