Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between ReadOnlyCollection<T> and ReadOnlyCollectionBuilder<T> in .Net?

Today I came across a dilemma what is the difference between ReadOnlyCollection<T> and ReadOnlyCollectionBuilder<T> in .Net?

In ReadOnlyCollection<T> object we cannot add and remove items.

Where as in ReadOnlyCollectionBuilder<T> object we can add and remove items.

If we can add and remove items in ReadOnlyCollectionBuilder<T> object, then why the name readonly?

like image 516
Dharmesh Tailor Avatar asked Feb 21 '12 10:02

Dharmesh Tailor


1 Answers

As per the documentation, ReadOnlyCollectionBuilder is just a builder for read-only collections. The idea is that the builder is mutable, but would only be used in a small scope. You'd create a builder, add a load of elements, then call ToReadOnlyCollection to efficiently create a read-only view on the data. (This resets the builder, so you avoid the possibility of mutating the read-only collection after creation.)

like image 148
Jon Skeet Avatar answered Sep 30 '22 20:09

Jon Skeet