Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do AsReadOnly returns a copy of the collection?

In would like to know if this method returns a copy of the collection or just an instance of ReadOnlyCollection which will wrap the original collection in some way, without readding all references.

I must understand if it's going to waste memory space, will it copy all pointers to my instances?

Thanks for any answer.

like image 345
Francesco Belladonna Avatar asked Oct 24 '11 19:10

Francesco Belladonna


2 Answers

as you can find here: List(Of T).AsReadOnly Method

To prevent any modifications to List(Of T), expose List(Of T) only through this wrapper.

A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.

This method is an O(1) operation.

since in the last statement they say if you change the source collection the readonly collection will reflect those changes, I believe this wrapper does not re-allocate any object but does what you also described in your question.

like image 106
Davide Piras Avatar answered Sep 28 '22 18:09

Davide Piras


The AsReadOnly method on List<T> just returns a ReadOnlyCollection<T> wrapper over the existing List<T>. It doesn't copy the underlying List<T>

like image 34
JaredPar Avatar answered Sep 28 '22 18:09

JaredPar