关于只读属性

来源:互联网 发布:阿里云账号提现 编辑:程序博客网 时间:2024/05/17 01:24

刚才在csdn上看到一个帖子,学习到了一些东西,赶紧记录下

 

     public int[] TestArray
        {
            get
            {
                return new int[] { 2,3,4};
            }
        }

 

在使用这个数组的时候

 

TestProperty test=new TestProperty();

 

test.TestArray=new int[]{1};//编译失败,因为只读属性

 

但是

 

test.TestArray[1]=1;//这一编译通过,

因为虽然TestArray这个是引用类型,也就是说引用这个地址是只读的,但是这个地址里存的是一些其他的地址,这些地址里对应的内容是允许被修改的。