-3

I have the following code

public class Base {

    public Base() {}

    public virtual void IdentifyYourself() {
         Debug.Log("I am a base");
    }

    public void Identify() { this.IdentifyYourself(); }
}

public class Derived : Base {

    public Derived() {}

    public override void IdentifyYourself() {
        Debug.Log("I am a derived");
    }
}

I run the following test code in a different entrypoint:

Base investigateThis = new Derived();
investigateThis.Identify()

and the output is: "I am a derived"

So no matter where the C# 'this' keyword is used; does it always refer to the run-time type no matter what scope 'this' is used in?

Bonus points to anyone who was able to 'Google' better than me and find MSDN documentation on specifically 'this' (pun intended) behavior.

Lastly, does anyone happen to know what is happening under the hood? Is it just a cast?

Update #1: Fixed typo in code; With the current set of answers, I guess I did not fully understand the implications of what the MSDN documentation meant by "..is the current instance..".

Update #2: Apologies, I wasn't sure if I should have made a separate question, but on further investigation, I've confused myself again; given this updated code, why is the output both: "I am a derived" & "It is a base!".

Didn't other people answer that 'this' is indeed the run-time type? Let me know if my updated question still is not clear.

Updated code:

public class Base {

    public Base() {}

    public virtual void IdentifyYourself() {
         Debug.Log("I am a base");
    }

    //Updated the code here...
    public void Identify() { this.IdentifyYourself(); AnotherTake(); }

    public void AnotherTake() { WhatIsItExactly(this); }

    public void WhatIsItExactly(Derived thing) {
         Debug.Log("It is a derived!");
    }

    public void WhatIsItExactly(Base thing) {
         Debug.Log("It is a base!");
    }
}

public class Derived : Base {

    public Derived() {}

    public override void IdentifyYourself() {
        Debug.Log("I am a derived");
    }
}
AMemberofDollars
  • 331
  • 2
  • 14

3 Answers3

1

Absolutely! investigateThis refers to an instance of Derived.

So the virtual method IdentifyYourself in Derived will be called. This is run-time polymorphism in effect.

The scope does not matter.

Under the hood, an virtual function table is built, and there is a pointer in the object that points to that table.

0

'this' always refers to the current instance, whereas 'base' always refers to the inherited type, regardless of whether the code using 'this' is in the base or the child class, it will always refer to the child (unless the base is instantiated itself, of course). It's just a reference to the current instance, like 'self' in python. Useful if a parameter has the same name as a private field, but as far as I'm aware it has no functional purpose other than that, I use it for readability, to clearly show when something belongs to the class I'm working in.

Callum Bradbury
  • 936
  • 7
  • 14
0

if you google: c# this the following link is the first result returned.

https://msdn.microsoft.com/en-us/library/dk1507sz.aspx

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

You may also want to take a look at base while you are at it.

https://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx

The base keyword is used to access members of the base class from within a derived class:

  • Call a method on the base class that has been overridden by another method.

  • Specify which base-class constructor should be called when creating instances of the derived class.

Nkosi
  • 235,767
  • 35
  • 427
  • 472