Runtime: Support Readonly Multidimensional Array

Created on 2 Jul 2018  路  5Comments  路  Source: dotnet/runtime

Suppose I have a multidimentional array

int[,] mdim

With a regular array int[] arr, I can do Array.AsReadOnly(arr) to get a readonly wrapper to protect my inner data when exposing outside of my class.

I'd like to be able to do the same with my multidimensional array: Array.AsReadOnly(mdim)

api-needs-work area-System.Collections

Most helpful comment

I believe that this request belongs on CoreCLR or CoreFX as this doesn't involve changes to the C# language but instead adding new overloads to the Array class to support multidimensional arrays.

All 5 comments

I believe that this request belongs on CoreCLR or CoreFX as this doesn't involve changes to the C# language but instead adding new overloads to the Array class to support multidimensional arrays.

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley
See info in area-owners.md if you want to be subscribed.

@hawkerm This is a nice coincidence 馃槃
Would this issue you're having be solved by using ReadOnlyMemory2D<T> and ReadOnlySpan2D<T> from the Microsoft.Toolkit.HighPerformance package (https://github.com/windows-toolkit/WindowsCommunityToolkit/pull/3353)?

As in:

int[,] array = ...; // Something
ReadOnlyMemory2D<int> memory = array;

// Use the memory or the span from there where needed

Note: I know it's not exactly the same as this wouldn't be a normal ReadOnlyCollection<T> instance, but at the same time it would also retain more info about the inner layout of the wrapped data. Not suggesting this as a definitive solution for the issue but I'm mostly just curious to know whether those APIs would be useful in this situation 馃槉

As @Sergio0694 mentioned we would probably need a dedicated type that is separate from ReadOnlyCollection<T>, presumably something that implements multi-dimensional indexers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nalywa picture nalywa  路  3Comments

jzabroski picture jzabroski  路  3Comments

jchannon picture jchannon  路  3Comments

Timovzl picture Timovzl  路  3Comments

bencz picture bencz  路  3Comments