问题
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.
回答1:
You should use a ReadOnlyCollection<int[]>.
来源:https://stackoverflow.com/questions/10808098/jagged-array-typed-properties