考虑以下代码:
public class Program
{
private static void Main(string[] args)
{
var person1 = new Person { Name = "Test" };
Console.WriteLine(person1.Name);
Person person2 = person1;
person2.Name = "Shahrooz";
Console.WriteLine(person1.Name); //Output: Shahrooz
person2 = null;
Console.WriteLine(person1.Name); //Output: Shahrooz
}
}
public class Person
{
public string Name { get; set; }
}
显然,当分配person1给person2且Name属性person2更改时,Nameof person1也将更改。person1并person2具有相同的参考。
为什么在时person2 = null,person1变量也不会为null?
守候你守候我
宝慕林4294392