如何在C#中传递“ this”

eba*_*all 9 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的假设是,当行:

Person secondPerson = firstPerson.myself;

运行时,该secondPerson变成第一人称的引用,所以当我将名称更改为“条例”,firstPerson.namesecondPerson.name都是比尔。但是当我跑步时

firstPerson.myself = new Person();

我希望firstPerson.namesecondPerson.name回到“埃迪”,但它仍然是“条例”。为什么?提前致谢!

Gra*_*ICA 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)
Run Code Online (Sandbox Code Playgroud)


Pra*_*kar 6

this 表示一个类的当前实例。

在创建Person的新实例时firstPerson.mySelf,该时间将引用Personclass 的新实例。

Person firstPerson = new Person();
Person secondPerson = firstPerson.myself; //Here you are referencing to same instance of Person class i.e. same `this`
Run Code Online (Sandbox Code Playgroud)

但是,当您创建的新实例Person,它将引用newthis

firstPerson.myself = new Person();  // New instance new `this`, but still `firstPerson` is referencing to previous instance
Run Code Online (Sandbox Code Playgroud)

图表说明

在此处输入图片说明

在您的情况下,您创建了person的新实例并存储在myself property中。但firstPersonsecondPerson仍然指向同一个this实例

  • @Grant的解释比我更清楚,我同意,但是我试图向您解释一下它以及它在您的程序中如何工作..但我完全同意公认的答案是解释您程序行为的最简单方法,+ 1是你授予 (2认同)