Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly wrapper of a LinkedList. HowTo?

.NET v2

When the List has a very useful (4 me) method AsReadOnly() the LinkedList does not have such a method.

Is there a way to "quickly" interface an internal LinkedList to read only from the external code?

like image 501
serhio Avatar asked Dec 13 '22 01:12

serhio


1 Answers

Why not just return a IEnumerable<T>? If you just want to let users enumerate the list without modifying it*, IEnumerable is the obvious choice.

If you want to have a read only interface of the LinkedList interface, you can wrap LinkedList, forward read only methods to the wrapped list and deny any changes.

*) Keep in mind that neither ReadOnlyCollection not IEnumerable will prevent callers to change state of objects in case of a collection of reference types. If the objects should be read only as well, you need to implement this as part of their type.

like image 120
Brian Rasmussen Avatar answered Dec 16 '22 17:12

Brian Rasmussen