代码一: class test { public int num = 0; } class Program { static void Main(string[] args) { test testA = new test(); testA.num = 10; test testB = testA; Console.WriteLine(testB.num.ToString()); Console.ReadKey(); } } 输出结果为10; 代码二: class test { public int num = 0; } class Program { static void Main(string[] args) { test testA = new test(); test testB = testA; testB.num = 100; Console.WriteLine(testA.num.ToString()); Console.ReadKey(); } }输出结果为100;代码三: class test { public int num = 0; } class Program { static void Main(string[] args) { test testA = new test(); test testB = testA; testB.num = 100; testB = null; Console.WriteLine(testA.num.ToString()); Console.ReadKey(); } }输出结果为100; testB 对象为空。
代码一里把 testA.num = 10;放到 test testB = testA; 之后好理解我说的。
你是不是想问为什么 testA.num = 10; test testB = testA; 之后testB .num还是没有值,这就是浅拷贝和深拷贝的区别或者说我理解错了你的问题?
代码一写错了点,是这样的。 class test { public int num = 0; } class Program { static void Main(string[] args) { test testA = new test(); test testB = testA; testA.num = 100; Console.WriteLine(testB.num.ToString()); Console.ReadKey(); } } 不过这不是我要问的。 我问的是代码三, testB 指向的是testA 的堆栈地址, testB现设为null了,难道不是把 testA 堆栈地址里也设为null 而只是把testB设为了null?
test testA = new test();test testB = testA; testB =null; testA也是null吧
应该:源类指向的变量为null了,所谓源类(说对象好一点,不该是类)没有变。
代码一:
class test
{
public int num = 0;
} class Program
{
static void Main(string[] args)
{
test testA = new test();
testA.num = 10; test testB = testA; Console.WriteLine(testB.num.ToString());
Console.ReadKey();
}
}
输出结果为10;
代码二:
class test
{
public int num = 0;
} class Program
{
static void Main(string[] args)
{
test testA = new test(); test testB = testA; testB.num = 100; Console.WriteLine(testA.num.ToString());
Console.ReadKey();
}
}输出结果为100;代码三:
class test
{
public int num = 0;
} class Program
{
static void Main(string[] args)
{
test testA = new test(); test testB = testA; testB.num = 100;
testB = null; Console.WriteLine(testA.num.ToString());
Console.ReadKey();
}
}输出结果为100;
testB 对象为空。
testA.num = 10;放到
test testB = testA;
之后好理解我说的。
testA.num = 10;
test testB = testA;
之后testB .num还是没有值,这就是浅拷贝和深拷贝的区别或者说我理解错了你的问题?
{
public int num = 0;
} class Program
{
static void Main(string[] args)
{
test testA = new test(); test testB = testA; testA.num = 100; Console.WriteLine(testB.num.ToString());
Console.ReadKey();
}
}
不过这不是我要问的。
我问的是代码三,
testB 指向的是testA 的堆栈地址,
testB现设为null了,难道不是把 testA 堆栈地址里也设为null
而只是把testB设为了null?
test testA = new test();test testB = testA;
testB =null;
testA也是null吧
正常输出100;
按你说的源类(其实是源对象)传递给类a(a对象),也就是源和a地址相同,指向同一个对象;然后你将源改为null,也就是将它的地址清空,这时a并未变
楼主看代码三 test testA = new test();
//这里,1.在栈上申明一个test类型的引用testA;2.在托管堆上实例化一个test类;3.textA中存放的
//是test实例的入口地址.test testB = testA;
//再在栈上申明一个test类型的引用testB,并且把testA中存放的入口地址也赋个testB,此
//时,testA/testB指向的是托管堆中的同一块地址(脑子里想象一下,栈上有两个引用,指向堆的同一个地方)testB.num = 100;
//其实是对那个实例化出来的test的num赋值100testB = null;
//此时,我testB不再指向test的那个实例的,我给它赋个NULL,空地址
//你想想,这儿我只是对testB的操作,和托管堆上的那个test实例以及testA又有什么关系呢
//此时,testA依旧是指向test那个实例
//就像我两根线连着一个球,我剪断了其中一根,会对另一根线和那个球造成什么影响呢??所以testA.num依旧是100.