我试图了解“ this”如何作为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”。为什么?预先感谢!
答案 0 :(得分:7)
您已更改了Person
指向的firstPerson.instance
实例,但没有更改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)
答案 1 :(得分:6)
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
的新实例时,它将引用新的this
firstPerson.myself = new Person(); // New instance new `this`, but still `firstPerson` is referencing to previous instance
说明图
在您的情况下,您创建了person的新实例并存储在
myself
中 属性。但firstPerson
和secondPerson
仍指向 相同的this
实例
答案 2 :(得分:3)
myself
只是一个变量。所以当你打电话
Person firstPerson = new Person();
您有两个指向相同实例的变量:firstPerson
和firstPerson.myself
。与线
Person secondPerson = firstPerson.myself;
您引入了第三个变量,该变量仍然指向同一实例。现在有了
firstPerson.myself = new Person();
您创建第二个实例,并使firstPerson.myself
指向该实例,而变量firstPerson
和secondPerson
仍指向第一个实例。
答案 3 :(得分:0)
1。实际上,类变量是引用类型。
2。因此,当您将同一实例分配给两个变量时,它们将指向同一实例。
3。每当您指向新内容时,都需要使用'new'关键字进行分配。