Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jagged Array typed Properties

Say I have a property like this:

public int[] MyProperty
{
    get;
    set;
}

The calling code is free to change the values of the array, but also to replace the array itself. This can easily be prevented by hiding the setter, like so:

public int[] MyProperty
{
    get;
    private set;
}

This allows the calling code to change the values, but it can't change the array to a different one, or change the size.

This pattern works with multidimensional arrays, but not jagged arrays. If I have this:

public int[][] MyProperty
{
    get;
    private set;
}

The main array can't be modified, but the child arrays can. What if I want to make the child arrays readonly, so that the calling code can change the elements, but not the arrays?

How can I make only the elements of jagged arrays modifiable? This includes 3D and higher.

like image 712
Kendall Frey Avatar asked Mar 03 '26 06:03

Kendall Frey


1 Answers

You should use a ReadOnlyCollection<int[]>.

like image 193
SLaks Avatar answered Mar 05 '26 22:03

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!