zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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

一个 类型 引用 有趣 问题
2023-09-11 14:21:23 时间
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会重定向,从而切断了引用的映射关系。