小编eba*_*all的帖子

如何在C#中传递“ 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.name …

c# properties this

9
推荐指数
2
解决办法
423
查看次数

标签 统计

c# ×1

properties ×1

this ×1