C# Collections of Entities

Collections are a list or a container which can hold entities. Each have different use cases.

Interface Collections That Support Iteration

IEnumerable

This is the most basic type of container. Exposes an enumerator, which supports a simple iteration over a non-generic collection. You cannot add, delete or update and there are no count methods, to get a count you would need to iterate over all entities.

1
2
// Methods
public System.Collections.IEnumerator GetEnumerator ();

ICollection

Extends IEnumerable and defines size, enumerators, and synchronization methods for all non-generic collections. Has functionality to add, remove, update entities in the container and get the count.

1
2
3
4
5
6
7
// Properties
public int Count { get; }
public bool IsSynchronized { get; }
public object SyncRoot { get; }

// Methods
public void CopyTo (Array array, int index);

IList

Extends IEnumerable and represents a non-generic collection of objects that can be individually accessed by index, it supports the same functionality to add, remove, update entities in the container.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Properties
public bool IsFixedSize { get; }
public bool IsReadOnly { get; }
public object this[int index] { get; set; }

// Methods
public int Add (object value);
public void Clear ();
public bool Contains (object value);
public int IndexOf (object value);
public void Insert (int index, object value);
public void Remove (object value);
public void RemoveAt (int index);

Other Interface Collections

IReadOnlyCollection

Represents a strongly-typed, read-only collection of elements.

IReadOnlyList

Represents a read-only collection of elements that can be accessed by index.