I have a Collection<T> property that wraps a array like
T[] array;
public Collection<T> Items
{
get { return new Collection<T>(array); }
}
When I attempt to assign to the collection via:
T variable;
Items[i] = variable;
I get a NotSupportedException because the colleciton’s IsReadOnly property is true. Turns out that this is a design choice by Microsoft. Does anyone know a workaround that does NOT involve enumeration? It could be done if the underlying data is not an array but I enjoy the performance gains because the data is fixed length.
No there is no way around this. Arrays implementation of
Collection<T>fits the read only model which means it will throw on attempted writes.If you need to have a mutable collection then I would suggest using a
List<T>under the hood instead of an array. You can force it to have the same initial size as well.