I have a class which must implement the following property
public ICollection<IType> Items
{
get { return this.items;}
}
My question is how to implement this when the type of this.items is a List<MyType> where MyType implements IType. I need to ensure the following:
- Unecessary enumeration of the list is avoided if possible
- That the class can internally treat the elements of
this.itemsas their concrete type - That external callers may add and remove elements to this collection
Thanks in advance.
How about
ItemsbeingIEnumerable<IType>? IEnumerable is covariant so the code would just work with no changes. On the other hand, you could have another, dedicated method to add elements to the internal list.