Replies: 3 comments 5 replies
-
|
Beta Was this translation helpful? Give feedback.
-
Changing array would definitely be non-starer. Inserting a frozen-check on write accesses is a nightmare beyond covariance check. Introducing new type for frozen array is also very heavy for the whole ecosystem. It may be covered by supporting user-defined variable-length object, but it would need more complex work and limitations. |
Beta Was this translation helpful? Give feedback.
-
While a frozen array feature could be hypothetically added, trying to avoid allocating a wrapper object when using collection expressions is probably not a good enough reason to add them. If you want to avoid the extra allocation, surely you also want to avoid the interface dispatch to access each element of the array. The code can be changed to use a Ignoring the cost/benefit of such a feature stated above, I can think of a couple ways of implementing it. One would be to add run time checks when writing to arrays to make throw an exception if the array is frozen. These checks might not be too costly (they could be lifted out of loops like range checks are), but they probably would add overhead. Another way to implement a frozen array would be to have the GC store the array in read only memory (memory marked read only by |
Beta Was this translation helpful? Give feedback.
-
Currently C# collection expressions generate a wrapper class around T[] to avoid cast/mutation on
IEnumerable<T>
,IReadOnlyList<T>
,IReadOnlyCollection<T>
. Could runtime support "frozen" array instances to avoid this extra allocation for each instance?Beta Was this translation helpful? Give feedback.
All reactions