Reputation: 4399
I have a method that takes a collection of type float as an argument. Most of the time a ReadOnlyCollection<float>
is passed to the method but sometimes different types of collections like a List are also passed.
What would be the best type for the parameter? Should I use: public void MyMethod(Collection<float>)
? Is it good practice?
Upvotes: 2
Views: 76
Reputation: 499042
It depends on what you are doing with the collection.
If you just want to enumerate over it, pass in IEnumerable<float>
. If you need to also add/remove items, use ICollection<float>
. If you need to access by index, use IList<float>
.
From your description of the different types, you do not need to add items, so IEnumerable<float>
should be fine.
In general, use the most general type you can (interface over implementation, something with less methods than one with more). See the Liskov substitution principle (the L in the SOLID design principles).
Upvotes: 6