Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is "this" passed in C#

I'm trying to understand how "this" gets passed around as a property in 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();
    }
}

My assumption is that when the line:

Person secondPerson = firstPerson.myself;

is run, that secondPerson becomes a reference to firstPerson, so when I change the name to "Bill", firstPerson.name and secondPerson.name are both Bill. But when I run

firstPerson.myself = new Person();

I expected firstPerson.name and secondPerson.name to go back to "Eddie", but it remains "Bill". Why? Thanks in advance!

like image 720
eball Avatar asked May 21 '19 16:05

eball


Video Answer


1 Answers

You've changed the Person instance that firstPerson.instance is pointing to, but not the original instance that firstPerson refers to.

So firstPerson is still pointing to the original Person instance (and so firstPerson.name returns the value set in the first instance), while firstPerson.instance is now pointing to a new (second) Person instance.

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)
like image 137
Grant Winney Avatar answered Oct 20 '22 18:10

Grant Winney