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*

9
  • 7
    If you're not planning to mutate the collection, I'd think the intent can better be shown with ToImmutableArray() (from the System.Collections.Immutable package) 😉 Commented Feb 7, 2020 at 14:49
  • 7
    Thanks for this. The chosen answer is a mere argument and assuming results following that argument. To do this scientifically and as a bonus know how much of a difference there is, there's only one real way to know. Commented May 13, 2020 at 12:22
  • 5
    Could this test suffer from repeatedly running with the same length of the arrays? That would mean that buffers of the right size are available in LargeArrayBuilder Commented Jan 26, 2021 at 8:44
  • 10
    This doesn't take into account that RangeIterator.ToArray and RangeIterator.ToList implementations know the final size of the collection. With any more complex LINQ expressions (e.g. with Where) the size can't be known and the result collection will have to be dynamically resized. So this answer is valid only for a small subset of real-world cases. Commented Mar 17, 2021 at 4:40
  • 9
    @ErikOvegård @Tyrrrz @Jonas @neonblitzer I modified the benchmark code to: public IEnumerable<int> Items => Enumerable.Range(0, Count).Where(i => i % 17 == 0); and in most of the cases ToList is around 10% faster, with less allocations. Commented Oct 14, 2021 at 22:23