IEnumerable, ICollection and IList are interfaces in the .Net Framework used the most often. IEnumerable is the base of the ICollection and IList interfaces (and many other). All these interfaces provide various functionalities and are useful in various cases.
IEnumerable Interface
IEnumerable interface is used when we want to iterate among our classes using a foreach loop. The IEnumerable interface has one method, GetEnumerator, that returns an IEnumerator interface that helps us to iterate among the class using the foreach loop. The IEnumerator interface implements two the methods MoveNext() and Reset() and it also has one property called Current that returns the current element in the list.
I have created a class StoreData for holding an integer type of data and this class implements the IEnumerable interface. Internally I have used a linked list for holding the data (you can find the advantages of a linked list from my previous article “http://www.c-sharpcorner.com/UploadFile/78607b/overview-of-linked-list/”).
- class StoreData : IEnumerable
- {
- LinkedList<int> items = new LinkedList<int>();
- public void Add(int i)
- {
- items.AddLast(i);
- }
- public IEnumerator GetEnumerator()
- {
- foreach (var item in items)
- {
- yield return item;
- }
- }
- }
- static void Main(string[] args)
- {
- StoreData list = new StoreData();
- list.Add(1);
- list.Add(2);
- list.Add(3);
- list.Add(4);
- foreach (var item in list)
- {
- Console.WriteLine(item);
- }
- Console.ReadLine();
- }
- IEnumerator enumerator = list.GetEnumerator();
- while (enumerator.MoveNext())
- {
- Console.WriteLine(enumerator.Current);
- }
Note
Every collection inside the .Net Framework implements the IEnumerable interface.
Key points of IEnumerable Interface
It provides read-only access to collections. We cannot change any item inside an IEnumerable List. It provides a sort of encapsulation in cases where we don't want our list to be changed.
If we are dealing with some SQL queries dynamically then it also provides lazy evaluation. That means the queries will not be executed until we explicitly need them.
ICollection Interface
The ICollection interface is inherited from the IEnumerable interface which means that any class that implements the ICollection interface can also be enumerated using a foreach loop. In the IEnumerable interface we don't know how many elements there are in the collection whereas the ICollection interface gives us this extra property for getting the count of items in the collection. The ICollection interface contains the following,
- Count Property
- IsSynchronized Property
- SyncRoot Property
- CopyTo Method
The Count property is used for maintaining the count of elements in the list whereas the IsSysnchronized and SyncRoot properties help to make the collection thread-safe. The CopyTo method copies the entire collection into an array.
The generic version of this interface provides Add and Remove methods also.
IList Interface
The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to add items to and remove items from the collection. It also provides support for accessing the items from the index. This interface has more power than the preceding two interfaces.
The IList interface contains the following,
- IsFixedSize Property
- IsReadOnly Property
- Indexer
- Add Method
- Clear Method
- Contains Method
- Indexof Method
- Insert Method
- Remove Method
- RemoveAt Method
The IList interface has one indexer by which we can access any element by its position and can insert an element and remove an element at any position.
No comments:
Post a Comment