Interfaces are mainly used as parameters or return types in functions that support I
in SOLID
Design principles (the principle of segment separation) to simplify the casting process, to support the transfer of several specific types to conjugate parameters and to hide (encapsulate) the actual parameter type from clients of your public functions .
Very simple, it produces a counter that supports simple iteration over a non-shared collection.
[ComVisibleAttribute(true)] public interface IEnumerable
- Here's about
ICollection
:
The ICollection interface extends IEnumerable; IDictionary and IList are more specialized interfaces that extend ICollection. An IDictionary implementation is a collection of key / value pairs, such as the Hashtable class. An IList implementation is a set of values, and its members can be accessed by index, such as the ArrayList class. Some collections that restrict access to their items, such as the Queue class and the Stack class, directly implement the ICollection interface. If neither the IDictionary nor the IList interface meet the requirements of the required collection, for greater flexibility, derive a new collection class from the ICollection interface.
[ComVisibleAttribute(true)] public interface ICollection : IEnumerable
Here's roughly the IList
interface:
[ComVisibleAttribute (true)] public interface IList: ICollection, IEnumerable
More from MSDN about IList: IList
is a descendant of the ICollection
interface and is the base interface of all non-shared lists. IList implementations fall into three categories: read-only, fixed, and variables. A read-only list can not be modified. Fixed-size IList does not allow you to add or remove elements, but allows you to modify existing elements. Variable-sized IList lets you add, delete, and modify elements.
Tip:
If you just want to support foreach
for your parameter collection, IEnumerable
should be enough. If you also want support for adding and removing items from the collection, IList
best choice.
Suggestions: This blog post: IEnumerable, ICollection, IList Compared will certainly help you make the best and most accurate decision.
Also, look at this article for performance and other comparisons of IList and IEnumerable.