[英]C# Classes reference types
I have a doubt on how reference types actually work. 我对引用类型实际上如何工作有疑问。 I have a class Person with two properties Name and Age.
我有一个具有两个属性Name和Age的Person类。 I am creating an object of Person class (objPerson1), assigning some values to both the properties and assigning that object to another of type Person (objPerson2).
我正在创建一个Person类的对象(objPerson1),为这两个属性分配一些值,并将该对象分配给另一个Person类型的对象(objPerson2)。 Now the question is after assigning when I change the Name and Age property and print them both the object shares same Name and Age which is fine as they are reference type.But when I assign null value to object itself then other object doesn't get nullified.Below is the code
现在的问题是分配后,当我更改Name和Age属性并打印它们时,两个对象共享相同的Name和Age,这与它们是引用类型一样好。但是当我为对象本身分配null值时,其他对象不会得到无效。下面是代码
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void Main(string[] args)
{
Person objPerson1 = new Person();
objPerson1.Name = "Tom";
objPerson1.Age = 10;
Person objPerson2 = objPerson1;
objPerson2.Name = "Jerry";
objPerson2.Age = 15;
Console.WriteLine($" Person1-{objPerson1.Name},{objPerson1.Age} and Person2-{objPerson2.Name},{objPerson2.Age}");
//Above line prints Person1-Jerry,15 and Person2-Jerry,15
//which is right as both are sharing same address.But when I wrote following code it confused me alot.
}
public static void Main(string[] args)
{
Person objPerson1 = new Person();
objPerson1.Name = "Tom";
objPerson1.Age = 10;
Person objPerson2 = objPerson1;
objPerson2 = null;
//After executing above line objPerson2 was null but objPerson1 were still having the values for Name and Age.
}
As they are reference type and both pointing to same address if I assign null to objPerson2 ,objPerson1 should also be null and vice-versa.Correct me if I'm wrong 因为它们是引用类型,并且如果我为objPerson2指定null时都指向相同的地址,那么objPerson1也应该为null,反之亦然。如果我错了,请纠正我
A bit simplified, but hopefully sufficient for you to understand: 有点简化,但希望足以让您了解:
Person objPerson1 = new Person();
Heap: memory allocated for object 堆:为对象分配的内存
Stack: objPerson1
= address of the heap object 堆栈:
objPerson1
=堆对象的地址
objPerson1.Name = "Tom";
objPerson1.Age = 10;
Heap: is being filled with values. 堆:正在被值填充。
Stack: unchanged (still the same address) 堆栈:不变(仍为相同地址)
Person objPerson2 = objPerson1;
Stack: another variable gets the same address 堆栈:另一个变量获取相同的地址
Heap: unchanged 堆:不变
objPerson2 = null;
Stack: the variable objPerson2
gets the value 0x00000000
. 堆栈:变量
objPerson2
的值为0x00000000
。
Note that objPerson1
still has the address of the heap and the object on the heap still exists. 请注意,
objPerson1
仍然具有堆的地址,并且堆上的对象仍然存在。 So objPerson1
still "works". 因此
objPerson1
仍然“有效”。
objPerson2
is only a pointer to the memory allocated by the initialization of objPerson1
. objPerson2
只是指向由objPerson1
初始化分配的内存的指针。 Assigning null to objPerson2
removes this pointer. 为
objPerson2
分配null会删除此指针。 objPerson1
still points to that memory therfore it holds it's value and does not become null
once objPerson1
does. objPerson1
仍指向该内存,然后再保存该内存的值,并且一旦objPerson1
指向该内存就不会为null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.