如何在C#中传递“ this”

时间:2019-05-21 16:46:20

标签: c# properties this

我试图了解“ 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.namesecondPerson.name都是Bill。但是当我运行

firstPerson.myself = new Person();

我希望firstPerson.namesecondPerson.name回到“ Eddie”,但它仍然是“ Bill”。为什么?预先感谢!

4 个答案:

答案 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

说明图

enter image description here

  

在您的情况下,您创建了person的新实例并存储在myself中   属性。但firstPersonsecondPerson仍指向   相同的this实例

答案 2 :(得分:3)

myself只是一个变量。所以当你打电话

Person firstPerson = new Person();

您有两个指向相同实例的变量:firstPersonfirstPerson.myself。与线

Person secondPerson = firstPerson.myself;

您引入了第三个变量,该变量仍然指向同一实例。现在有了

firstPerson.myself = new Person();

您创建第二个实例,并使firstPerson.myself指向该实例,而变量firstPersonsecondPerson仍指向第一个实例。

答案 3 :(得分:0)

1。实际上,类变量引用类型

2。因此,当您将同一实例分配给两个变量时,它们将指向同一实例。

3。每当您指向新内容时,都需要使用'new'关键字进行分配。