Introduction
.NET framework provides interfaces that implements by collections in language to provide functionality of iterating over objects in collection, adding and removing object from collection to randomly access object from collection.
As different interfaces provide different set of functionality most of the developers has problem when to use which interface to achieve functionality. The following post provides information about interfaces implemented by collection.
Interfaces
The following diagram is for relation between the interfaces.
Note:
Note:
In above diagram (Inherited), the columns indicate that the features are inherited from parent and to find out from which parent one must look in the interface collection diagram.
So from above table three main interfaces functionality concluded in following way:
IEnumerable – interface provide minimum functionality which is Enumration.
ICollection – interface provide medium functionality which is getting size, adding, removing and clearing collection i.e. modification of collection. As it inherited from IEnumerable so includes functionality of IEnumerable.
IList – interface provide full functionality which is index base accessing of collection element, index base adding, index base removing from collection. As it inherited from ICollection it includes functionality of Enumerable and ICollection.
The following are some important things to know:
Conclusion
It’s very important for developers to understand these interfaces because the rule says its always good to depend on interface rather than on the concrete type.
.NET framework provides interfaces that implements by collections in language to provide functionality of iterating over objects in collection, adding and removing object from collection to randomly access object from collection.
As different interfaces provide different set of functionality most of the developers has problem when to use which interface to achieve functionality. The following post provides information about interfaces implemented by collection.
Interfaces
The following diagram is for relation between the interfaces.
Note:
- Class diagram are not having all the methods but contains important method that belongs to each collection interface.
- Collection interface is available in both generic and non-generic form, so in diagram obj type is object in nongeneric form and obj type is T(template type) in generic form.
Functionality Provided | Read | Count | Add & Remove | Index Based Read | Index Based Add & Remove | |
IEnumerable |
|
Y | N | N | N | N |
ICollection |
|
Y Inherited) |
Y | Y | N | N |
IReadOnlyCollection |
|
Y (Inherited) |
Y | N | N | N |
IList |
| Y (Inherited) |
Y (Inherited) |
Y (Inherited) |
Y | Y |
IReadOnlyList | Allow to read collection by Index. | Y (Inherited) |
Y (Inherited) |
N | Y | N |
Note:
In above diagram (Inherited), the columns indicate that the features are inherited from parent and to find out from which parent one must look in the interface collection diagram.
So from above table three main interfaces functionality concluded in following way:
IEnumerable – interface provide minimum functionality which is Enumration.
ICollection – interface provide medium functionality which is getting size, adding, removing and clearing collection i.e. modification of collection. As it inherited from IEnumerable so includes functionality of IEnumerable.
IList – interface provide full functionality which is index base accessing of collection element, index base adding, index base removing from collection. As it inherited from ICollection it includes functionality of Enumerable and ICollection.
The following are some important things to know:
- IEnumerable interface under the hood make use of IEnumerator for providing reaonly and forward mode read.
- IReadOnly*** and IEnumerable are used for providing readonly collection. But difference is that IEnumerable allows collection to read in forward only mode where IReadOnly*** provide feature of Collection /List but only in readonly mode i.e. without modification feature like add & remove.
- IReadOnly is part of collection interface from framework 4.5.
Conclusion
It’s very important for developers to understand these interfaces because the rule says its always good to depend on interface rather than on the concrete type.