Use ReadOnlyCollection<T>
. It is read-only and, contrary to what you believe, it has an indexer.
Arrays are not immutable and there is no way of making them so without using a wrapper like ReadOnlyCollection<T>
.
Note that creating a ReadOnlyCollection<T>
wrapper is an O(1) operation, and does not incur any performance cost.
Update
Other answers have suggested just casting collections to the newer IReadOnlyList<T>
, which extends IReadOnlyCollection<T>
to add an indexer. Unfortunately, this doesn't actually give you control over the mutability of the collection since it could be cast back to the original collection type and mutated.
Instead, you should still use the ReadOnlyCollection<T>
(the List<T>
method AsReadOnly()
, or Array
s static method AsReadOnly()
helps to wrap lists and arrays accordingly) to create an immutable access to the collection and then expose that, either directly or as any one of the interfaces it supports, including IReadOnlyList<T>
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…