1

If I have a very simple class like so:

public class Example
{
    public int Number { get; set; }
    public Example(int numberArg)
    {
        this.Number = numberArg;
    }
}

And I have a derivative class like so:

public class AnotherExample : Example
{
    public int DoubleNumber
    {
        get
        {
            return this.Number * 2;
        }
    }
    public AnotherExample(int numberArg) : base(numberArg) {}
}

What difference is there, if any, if I use this.Number to refer to the AnotherExample objects base Number property than if I use base.Number to refer to it?

Thanks!

Sinatr
  • 20,892
  • 15
  • 90
  • 319
nabir
  • 1,447
  • 2
  • 12
  • 17

3 Answers3

3

If Number is not overridden there is no difference, as this.Number will resolve correctly in the chain of inheritance.

This also means that calling this.Member in a base class, if overridden in a derived class will again use the inheritance chain and find the derived implementation.


If it has been overridden, using base will always go to the base class's version of that property, whereas this.Number will first go to your overridden version.

If you plan on calling virtual members inside your constructors, please be aware of the general advice around it:

Virtual member call in a constructor

This answer is leaving out the explanation about member hiding as the OP example is simpler than that.

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Ahh I see, so say I do some work with the `Number` property in my derivative class's ctor but use `base.Number` to access it via a `get` property, the work will not be reflected? – nabir Feb 06 '14 at 11:48
  • @Kenneth has an example of what I was talking about, thanks for the information! – nabir Feb 06 '14 at 11:50
  • @nabir Oh, calling a base class constructor? Yes, I see. Mind that in his example, `this.Number` within `Example` will call into `AnotherExample.Number` and won't do anything as `set` is an empty implementation. – Adam Houldsworth Feb 06 '14 at 11:51
  • Thanks again @Adam Houldsworth, that was at the back of my mind for a while :P – nabir Feb 06 '14 at 11:54
2

In this case, there is no difference. In case you override it will take the value from the base class instead of the inherited class:

public class Example {
    public virtual int Number { get; set; }
    public Example(int numberArg) {
        this.Number = numberArg;
    }
}

public class AnotherExample : Example {
    public override int Number 
    {
        get{return 5;}
        set{}
    }
    public int DoubleNumber {
        get {
            return this.Number * 2; // returns 10
            return base.Number * 2 // returns 2 times whatever the value is 
        }
    }
    public AnotherExample(int numberArg) : base(numberArg) {}
}
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
Kenneth
  • 28,294
  • 6
  • 61
  • 84
1

You can have a variable in derived class that has the same name with in the base class.In this case you need to use this and base keywords to avoid ambiguity.For example:

public class Base
{
    public int Number = 0;
}

public class Derived : Base
{
   public int Number;

   public Derived() 
   {
       this.Number = base.Number + 5;
   }
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184