3

I have been told that, a protected variable in the base class can be accessed in the derived class either using base or this keyword. I am totally confused now, because, this keyword refers to the current class (derived class).

public class A
{
  protected int i;
}
public class B :A
{
  void Display()
  {
    this.i=10;
    //  (or)
    base.i=10;
  }
}

Is there any technical difference ? First of all, today I am pondered to see why I cannot create an object of A inside Display() method and access the variable i through that object as the very definition of protected access says that, it can be accessed in any derived class. Folks, I have seen some threads, however what they say is, we cannot create object and access the protected members. I feel, this is pretty contrary to the definition.

Could someone can please explain me both of my above questions

leppie
  • 115,091
  • 17
  • 196
  • 297
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • *Is there any technical difference ?* No, neither `this.i` nor `base.i` will compile because `i` is defined in `A` and `B` is a different class. Did you mean to say `public class B : A`? – ta.speot.is Jun 29 '13 at 06:22
  • `this`,`base` in your code won't be able to access `i`..you are not inheriting A – Anirudha Jun 29 '13 at 06:22
  • Assuming you meant to inherit from `A` (but otherwise keep things the same) then there is no difference with the current code. However, if you were to add a field called `i` to class `B`, then `this.i` would refer to that field, whereas `base.i` would still refer to the base class version. – dlev Jun 29 '13 at 06:23
  • Your second question is answered here: http://blogs.msdn.com/b/ericlippert/archive/2005/11/09/why-can-t-i-access-a-protected-member-from-a-derived-class.aspx – dlev Jun 29 '13 at 06:24
  • @ta.speot.is: Hey good catch, sorry, typo. Corrected it :) Thanks – Jasmine Jun 29 '13 at 06:25
  • possible duplicate of [Difference between this and base and Best practices of use](http://stackoverflow.com/questions/3733234/difference-between-this-and-base-and-best-practices-of-use) – Anirudha Jun 29 '13 at 06:27
  • possible duplicate of [Is there any difference between 'base' and 'this' when referring to the parent object field, property or method?](http://stackoverflow.com/questions/3614559/is-there-any-difference-between-base-and-this-when-referring-to-the-parent-o) – nawfal Jun 29 '13 at 06:28
  • @dlev: Wow thank you so much, I experimented and got the difference between base and this in this context as you mentioned. But, I also have observed that, just 'i" without this or base keywords refers to B.i. Does that mean this.i =i ? – Jasmine Jun 29 '13 at 06:30
  • 2
    @Divine If you do not use either `this` or `base`, then `i` has an *implicit* `this` in front of it (unless there is also a *local variable or parameter* named `i`, which would take precedence.) – dlev Jun 29 '13 at 06:31
  • The `i` without a prefix is usually the same as `this.i` except when 'i' is defined in the immediate context (such as an argument to a method) -- dlev beat me to it. – mcmonkey4eva Jun 29 '13 at 06:32
  • @dlev: Thank you so much, that perfectly and quickly answered my question in the way I can chew :) I will have a look at your link on protected member access in derived class :) Thanks again!!! :) :) – Jasmine Jun 29 '13 at 06:35

2 Answers2

5

It's pretty simple:

this refers to the current object (EG, if a method is like void mymethod(int myvar), and the class has myvar defined in it, you can use myvar to refer to the argument, and this.myvar to refer to the the one in the class.)

base refers to the base class (The class your class is derived from). (EG, if you're overwriting a method Update, at the end of it, you can then call base.Update(); to call the original method.)

mcmonkey4eva
  • 1,359
  • 7
  • 19
0

Let's B extend A class, so, B inherite i of A:

public class A {
    protected int i;
}
public class B : A {
    void Display() {
        i = 10; // this.i
    }
}
Alex
  • 323
  • 2
  • 8
  • 2
    ... You appear to have posted this before you finished writing it. – mcmonkey4eva Jun 29 '13 at 06:23
  • 2
    How does that gain you more rep? If your answer is not as good as someone elses, I'm not going to +1 someone just because they make a 1st Post. – Patrick Magee Jun 29 '13 at 06:29
  • @PatrickMagee - the `edit it within 5 minutes` would be editing it to match someone else's answer, but maybe with a little bit of common-sense clarification. And uh... I don't think this guy's doing it, he just forgot to make his post make sense. – mcmonkey4eva Jun 29 '13 at 06:33