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

Today I am faced with a dilemma: what is the difference between ReadOnlyCollection<T> and ReadOnlyCollectionBuilder<T> in .Net?

In the ReadOnlyCollection<T> object, we cannot add or remove elements.

Where, as in the ReadOnlyCollectionBuilder<T> object, we can add and remove elements.

If we can add and remove elements in a ReadOnlyCollectionBuilder<T> , then why is the name read-only?

+7
source share
1 answer

According to the documentation , ReadOnlyCollectionBuilder is just a creator for read-only collections. The idea is that the builder is volatile, but will only be used to a small extent. You must create a builder, add a load of elements, and then call ToReadOnlyCollection to effectively create read-only data views. (This resets the builder, so you avoid the possibility of mutating the read-only collection after creation.)

+12
source

All Articles