繁体   English   中英

如何在C#中传递“this”

[英]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.namesecondPerson.name都是Bill。 但是当我跑步的时候

firstPerson.myself = new Person();

我期望firstPerson.namesecondPerson.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属性中。 firstPersonsecondPerson仍然指向同this实例

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

Person firstPerson = new Person();

你有两个指向同一个实例的变量: firstPersonfirstPerson.myself 随着线

Person secondPerson = firstPerson.myself;

你介绍了仍然指向同一个实例的第三个变量。 现在用

firstPerson.myself = new Person();

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

1.实际上类变量引用类型

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

3.无论何时你要指出什么都要新鲜,你需要使用'new'关键字进行分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM