Jagged Array typed Properties

笑着哭i 提交于 2019-12-10 20:03:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!