Function parameters: IEnumerable, ICollection or array?

If I have a function that accepts a collection of a specific type, what type of parameter should I use?
I previously suggested that IEnumerable is the default, but I would like to know if this is correct and if so, why. ICollection also seems to be a valid candidate (because of its name), but I get the impression that IEnumerables are more user friendly.
I decided to look at the examples that the frame gives us would be a good idea, but I found that something like String.Join is requesting an array of strings.

+6
source share
3 answers

You take the one that imposes the least on customers, but allows you to do your job. If you can get away with IEnumerable<T> over ICollection<T> , then you should use it because it gives more flexibility to your API clients.

+10
source

IEnumerable is preferred over ICollections. Here is a generous answer to your question.

+3
source

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 .

  • About IEnumerable :

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.

+1
source

Source: https://habr.com/ru/post/917779/


All Articles