class Numbers
{
List<int> num;
public Numbers()
{
num = new List<int> { 1, 2, 3, 4 };
}
public List<int> GetNumbers
{
get
{
return num;
}
}
}
class Program
{
static void Main(string[] args)
{
Numbers n = new Numbers();
List<int> l = n.GetNumbers;
n.GetNumbers[0] = 10; //Modification done in the original set.
foreach (int x in l)
{
Console.WriteLine(x);
}
Console.ReadLine();
}
}
为什么即使 GetNumbers 属性是只读的,原始数字列表 {1,2,3,4) 的值也会通过 GetNumbers 属性进行修改。