The ReadOnlyCollection(Of T) Collection

The System.Collections.ObjectModel.ReadOnlyCollection(Of T) is the read-only counterpart of the List(Of T) class. Being read-only, you can add items to the collection only when you create an instance, but then you cannot change it. The constructor requires an argument of type IList(Of T) so that the new collection will be generated starting from an existing one. The following code demonstrates how you can instantiate a new ReadonlyCollection:

image

As an alternative, you can create a ReadonlyCollection invoking the List(Of T).AsReadOnly method, as shown in the following code:

'Same as above Dim readonly As ReadOnlyCollection(Of ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.