Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

15
  • 7
    Lists and arrays perform almost exactly the same. The difference, if any, will be lost in the noise. But lists are just richer structures. And to use linq to handle missing features, that's the real, measurable overhead. Commented Jun 20 at 16:56
  • 3
    I think you miss the point of lazy evaluation/deferred execution that the abstractions IEnumerable<T> (and even more so IQueryable<T>) allow for. When you return List<T> or T[], you have to materialize the data. It sounds like you identify your work as performing exactly that materialization "reads data from some data source and sends back objects of that data to the caller" by hand-crafting each specific query. LINQ-to-SQL, EF, etc. can make such query code partially obsolete by allowing the client of your API to specify the query they need (and execute it on the data source) Commented Jun 21 at 14:06
  • 6
    For limiting the interface, especially mutability, you want IReadOnlyList<T>. Not arrays. Commented Jun 22 at 12:06
  • 1
    Speaking of (the lack of) type-safety and variance, arrays have a quirk in that you can assign a T[] to a variable of type object[], which then leads to problems (runtime exceptions) if you try to set anything other than T as an element. This behavior was introduced out of necessity into the language, before generics had support for covariance/contravariance (actually, now that I think about it, it could have been before the language had generics at all). So that's a potential pitfall. Other than that, the preference for List<T> over arrays is, I think, mostly cultural Commented Jun 22 at 22:25
  • 3
    If your code is creating a data structure from a stream of records (e.g. data retrieval from a database) you might be surprised to learn that ToArray() can actually perform worse than ToList. See this answer. Commented Jun 23 at 19:01