Collection wrapping a array is read-only. Possible to make it writeable without casting?

Posted by Brian Triplett on Stack Overflow See other posts from Stack Overflow or by Brian Triplett
Published on 2010-03-13T02:31:58Z Indexed on 2010/03/13 2:37 UTC
Read the original article Hit count: 299

Filed under:

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.

© Stack Overflow or respective owner

Related posts about c#