Returning read-only collection






3.60/5 (5 votes)
Another good idea is to return the list as enumerable using:return m_privateCollection.AsEnumerable();Now what are the differences?AsReadOnly() creates a completely new collection of list. Depending on the count of items and where the resource is, this may take a long...
Another good idea is to return the list as enumerable using:
return m_privateCollection.AsEnumerable();
Now what are the differences?
AsReadOnly()
creates a completely new collection of list. Depending on the count of items and where the resource is, this may take a long time.AsEnumerable()
returns exactly the items stored on the list, but one by one (probably usingyield
). Therefore adding and removing the items are not meaningful actions.AsEnumerable()
is lazy load. It means thatAsEnumerable()
won't query the items until the time you want to encounter the items. But methods likeAsList()
andAsReadOnly()
create the list exactly at the time you call them.