锯齿数组类型的属性 [英] 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?
如何仅使锯齿状数组的元素可修改?这包括3D及更高版本.
How can I make only the elements of jagged arrays modifiable? This includes 3D and higher.
推荐答案
You should use a ReadOnlyCollection<int[]>
.
这篇关于锯齿数组类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!