Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I return an array or a collection from a function?

What's the preferred container type when returning multiple objects of the same type from a function?

Is it against good practice to return a simple array (like MyType[]), or should you wrap it in some generic container (like ICollection<MyType>)?

Thanks!

like image 819
Mark Carpenter Avatar asked Mar 02 '09 16:03

Mark Carpenter


2 Answers

Eric Lippert has a good article on this. In case you can't be bothered to read the entire article, the answer is: return the interface.

like image 101
erikkallen Avatar answered Nov 15 '22 18:11

erikkallen


Return an IEnumerable<T> using a yield return.

like image 37
Daniel A. White Avatar answered Nov 15 '22 17:11

Daniel A. White