我试图了解“ 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 …