Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function parameters: IEnumerable, ICollection or array?

If I have a function that takes a collection of a certain type, what parameter type should I use?
I have previously assumed IEnumerable is the default, but I would like to know if this is correct and if so, why.
ICollection also seems like a valid candidate (due to its name), but I get the impression IEnumerables are more user friendly.
I figured looking at examples the framework gives us would be a good idea, but I find that something like String.Join, asks for an array of strings.

like image 237
Protector one Avatar asked Jun 11 '12 10:06

Protector one


2 Answers

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

like image 176
Kent Boogaart Avatar answered Oct 04 '22 00:10

Kent Boogaart


IEnumerable are more preferred over ICollections. Here is the generous answer to your question.

like image 40
vendettamit Avatar answered Oct 03 '22 22:10

vendettamit