如何把数组的某一个元素设置为只读?(C#)

来源:百度知道 编辑:UC知道 时间:2024/06/22 23:39:19
问一个高深的问题
如何把一个一维数组设置成只读。也就是说一开始往里赋值,之后这个值就变成了只读属性,不能再次往该数组里赋值

问题详细描述:
目的就是第一次保存住值,再一次赋值就赋不了值,而且值是变量。就想达到这个效果就行

还请高手明示。

建议把数组封装一下:(要是.net2.0)
using System;
using System.Collections.Generic;

public class MyClass
{
private int[] arr;

public MyClass()
{
arr = new int[3];
arr[0] = 44;
arr[1] = 55;
arr[2] = 53;
}

public int this[int index]
{
get
{
return arr[index];
}
}

public static void Main()
{
MyClass my = new MyClass();
//my[0] = 55; 这句赋值将不能执行,因为是只读的
Console.WriteLine(my[1]);

}
}

这个需求,需要使用 只读属性来实现。

private string[] a = new string[5];
//对a 赋值
public string[] A
{
get {return a;}
}

但是,使用的时候,使用的是 A。