C# 15 with(capacity) Syntax Outperforms Older List Pre-allocation Methods
A developer experimenting with C# 15 compared three ways to create a 100,000-item list: no pre-allocation, classic capacity constructor, and the new with(capacity) syntax. Without pre-allocation, lists repeatedly resize in memory as items are added, which can slow performance at scale. Benchmarks run on a personal laptop showed the C# 15 with(capacity) approach was consistently the fastest of the three methods. The new syntax combines capacity pre-allocation with collection expressions, making the code both more readable and more efficient. The informal experiment highlights how modern language features can offer performance gains even for fundamental operations like list creation.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in