引用类型(一个有趣的问题)

public static void Main(string[] args)
        {
            int N = 10;
            Test[] tests = new Test[N];
            for (int i = 0; i < N; i++)
            {
                tests[i] = new Test(i);
            }

Test test = tests[--N]; test.id = 10;           //这里没有问题,修改test.id会导致tests[i].id一起更改
test = tests[--N]; test = null;                 //这里有问题,修改test=null,但是  tests[i]没有变更
//Test test = new Test(11);                     //tests[9] = test; //test.id = 10; 
Console.WriteLine(test.id); 
Console.ReadLine(); 
} 
public class Test 
{ 
public int id; 
public Test(int id) { this.id = id; } 
}



这里,发现只有Null才会导致这个结果。

难道null比较特殊???

null会重定向,从而切断了引用的映射关系。


原文地址:https://www.cnblogs.com/hanjun0612/p/9779910.html