Top
image credit: Pixabay

.NET 6: Collections Improvements

June 4, 2021

Via: InfoQ

Before performing a large set of inserts into a Dictionary or HashSet, it is helpful to call EnsureCapacity with the expected collection size. This allows the collection to perform one resize operation upfront, avoiding the possibility of multiple resizes being needed.

The EnsureCapacity method has been added to the List<T>, Stack<T>, and Queue<T> classes so they too can gain the performance benefit.

A notable exclusion from this group is Collection<T>. Unlike the others, Collection<T> may optionally wrap another collection which may not necessarily expose an EnsureCapacity method. As a subclass of Collection<T>, ObservableCollection<T> cannot expose an EnsureCapacity method either.

Read More on InfoQ