IEnumerable, ICollection, IList, each interface is only suitable for some specific scenarios, how to use it differently?
The IEnumerable interface only provides a way to get iterators, which is why you can use a foreach traversal to implement the IEnumerable interface Collection.
public interface IEnumerable
{
IEnumerator GetEnumerator ();
}
The ICollection implements the IEnumerable interface, so that in addition to the ability to have IEnumerable interfaces, other capabilities are included.
public interface Icollection:ienumerable
{
int count{get;}
BOOL Issynchronized{get;}
Object Syncroot{get;}
IEnumerator GetEnumerator ();
void CopyTo (array array, int index);
}
IList implements both the ICollection and IEnumerable interfaces, which, on the basis of 2 interfaces, can add, remove, or empty the collection, and also provide access to the collection elements based on the index.
Public interface Ilist:icollection, IEnumerable
{
BOOL Isfixedsize{get;}
BOOL Isreadonly{get;}
Object This[int index] {get;set;}
int Add (Object value);
void Clear ();
BOOL Contains (Object value);
int IndexOf (Object value);
void Insert (int index, Object value);
void Remove (Object value);
void RemoveAt (int index);
}
Summarize:
If you only want to traverse the collection, use IEnumerable, ienumerable<t>
If you want to traverse, modify the collection, and the navigation properties that require lazy loading, use ICollection, icollection<t>
If you want to traverse, modify, add, empty, use an index, use IList, ilist<t>
How to choose to use IEnumerable, ICollection, IList