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.
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.
IEnumerable are more preferred over ICollections. Here is the generous answer to your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With