[英]How is “this” passed in C#
我试图理解“这个”如何作为C#-6.0(VS 2015)中的属性传递。
using System;
public class Person
{
private Person instance;
public Person()
{
instance = this;
}
public Person myself
{
get { return instance; }
set { instance = value; }
}
public string name = "Eddie";
}
public class Example
{
public static void Main()
{
Person firstPerson = new Person();
Person secondPerson = firstPerson.myself;
secondPerson.name = "Bill";
Console.WriteLine(firstPerson.name);
Console.WriteLine(secondPerson.name);
firstPerson.myself = new Person();
Console.WriteLine(firstPerson.name);
Console.WriteLine(secondPerson.name);
Console.ReadLine();
}
}
我的假设是当线:
Person secondPerson = firstPerson.myself;
运行时,secondPerson成为firstPerson的引用,所以当我将名称更改为“Bill”时, firstPerson.name
和secondPerson.name
都是Bill。 但是当我跑步的时候
firstPerson.myself = new Person();
我期望firstPerson.name
和secondPerson.name
回到“Eddie”,但它仍然是“Bill”。 为什么? 提前致谢!
您已经更改了firstPerson.instance
指向的Person
实例,但没有firstPerson
引用的原始实例。
因此, firstPerson
仍指向原始Person
实例(因此firstPerson.name
返回第一个实例中设置的值),而firstPerson.instance
现在指向新的(第二个) Person
实例。
Person firstPerson = new Person(); // instance 1
Person secondPerson = firstPerson.myself; // myself refers to instance 1
secondPerson.name = "Bill"; // set name in instance 1
Console.WriteLine(firstPerson.name); // get name from instance 1
Console.WriteLine(secondPerson.name); // get name from myself in instance 1
Console.WriteLine(firstPerson.myself.name); // get name from instance 1 (same as above)
firstPerson.myself = new Person(); // myself refers to instance 2, but firstPerson still refers to instance 1
Console.WriteLine(firstPerson.name); // still getting name from instance 1
Console.WriteLine(secondPerson.name); // still getting name from myself in instance 1
Console.WriteLine(firstPerson.myself.name); // get name from instance 2 (since firstPerson.myself was reassigned)
firstPerson = new Person(); // firstPerson and firstPerson.myself point to instance 3
Console.WriteLine(firstPerson.name); // get name from instance 3, which is the default "Eddie"
Console.WriteLine(secondPerson.name); // still points to instance 1, since that's what it was when it was assigned
Console.WriteLine(firstPerson.myself.name); // get name from instance 3 (since firstPerson.myself is defaults to the new instance again)
this
表示类的当前实例。
当您创建Person firstPerson.mySelf
新实例时,它将引用Person
类的新实例。
Person firstPerson = new Person();
Person secondPerson = firstPerson.myself; //Here you are referencing to same instance of Person class i.e. same `this`
但是当你创建Person
新实例时,它将引用new this
firstPerson.myself = new Person(); // New instance new `this`, but still `firstPerson` is referencing to previous instance
用图解释
在您的情况下,您创建了人的新实例并存储在
myself
属性中。 但firstPerson
和secondPerson
仍然指向同this
实例
myself
只是一个变数。 所以当你打电话
Person firstPerson = new Person();
你有两个指向同一个实例的变量: firstPerson
和firstPerson.myself
。 随着线
Person secondPerson = firstPerson.myself;
你介绍了仍然指向同一个实例的第三个变量。 现在用
firstPerson.myself = new Person();
你创建第二个实例并使firstPerson.myself
指向这个实例,而变量firstPerson
和secondPerson
仍然指向第一个实例。
1.实际上类变量是引用类型 。
2.因此,当您将同一实例分配给两个变量时,它们将指向同一个实例。
3.无论何时你要指出什么都要新鲜,你需要使用'new'关键字进行分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.