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?
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With