-3

Background

I have never had a formal class in OOP/OOD although I understand how the protected members work. Being accessible by any instance/derived class, they are thereby less private (more accessible) than private.

Now, what I am confused on... is how to access the protected members within the class for C#. I have seen examples of developers doing it in two different ways. The first way I have seen people doing it, is by calling on the keyword this in front of the member variable such as in this example here. The second way that I have seen people using protected member is by not calling on this and just calling the member itself. This tutorial over at tutorials point, does not use the this keyword.

Now, there's this question on StackOverflow. One of the answers states that "to qualify members hidden by similar name" does this include protected members?

Question

Are you supposed to use the this keyword on a protected member? I have seen it with/without.

  • Yes, the question and two topmost answers of your last link include protected members, they say all there is to say about this (pun intendend), – Doc Brown Apr 20 '16 at 13:15
  • 2
    This is purely opinion. For example Killian Foth's answer says you shouldn't. However, I always use this to access member variables as I think it's cleaner. – Thomas Owens Apr 20 '16 at 13:20
  • @ThomasOwens Good to know, why do you think it's cleaner? I guess in one way, it is an extra keyword... wouldn't that make it less clean? Or you mean maybe by adding the keyword, that if a parameter happens to have the same name in the future, you will have already used this. – Snoop Apr 20 '16 at 13:22
  • I'm not going to get into my personal thoughts here. The point is, different people are going to have different opinions. Such questions aren't a good fit for a Stack Exchange site. – Thomas Owens Apr 20 '16 at 13:23
  • 1
    There two things in this. Which is best to use this or not use this. That is a very opinionated discussion. The other part is if this is required for protected/private/public. Though this is a rather fundamental question on syntax and perhaps better suited for SO and there are probably duplicates there. It would be better close this as a duplicate of those and not as primarily opinion-based. – Bent Apr 20 '16 at 13:29
1

You should follow the coding guidelines where you work or on the projects you work on.

You will find that there are a lot of people who are totally for or totally against the use of this unless it is needed as in the example given in @KilianFoths answer.

It does not have anything to do with public, protected or private.

  • I guess I've always had variables of different names than the ones passed in anyways, so I'd have never needed this... and have used it for every single thing I've ever done. Good to know that I can change that from now on. – Snoop Apr 20 '16 at 13:18
4

No, you shouldn't use this redundantly. It adds unnecessary clutter and makes you look as if you're not sure what fields your class has.

The only reason that many people do this is that they often see this introductory example:

public Foo(Bar bar) {
    this.bar = bar;
}

and wrongly assume that you always have to use this, when in fact you only need it to disambiguate it against a local variable with the same name.

  • Oh okay, so it has nothing to do with public/private at all really? – Snoop Apr 20 '16 at 13:12
  • @StevieV: No, it has not. What makes you think it has? – Doc Brown Apr 20 '16 at 13:17
  • 2
    "It adds unnecessary clutter and makes you look as if you're not sure what fields your class has." - and the can of worms has been opened ;-) – Bent Apr 20 '16 at 13:19
  • It has a slight connection. Private/protected member fields are the ones that absolutely require constructor/accessor injection, which leads to the pattern above, which encourages excessive use of this. – Kilian Foth Apr 20 '16 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.