views:

119

answers:

1

I'm making an immutable struct in .Net which contains a read only collection of a different immutable struct (I have full control over the entire design). I don't need a non-mutating Add method.

What's the best way to do that?

I could make the outer struct have a reference to a ReadOnlyCollection containing the inner struct. Are there any other options?

+3  A: 

ReadOnlyCollection is one way.

Depending on what you need to do with the collection, you could also expose it as IEnumerable instead.

Cameron MacFarland
+1 You could use an simple array, but array are mutable. The `Array` class has a static method called `AsReadOnly` that converts arrays to read-only collections. It returns a `ReadOnlyCollection`. There's no reason to differ from that.
jpbochi