11

Possible Duplicate:
When do you use the “this” keyword?

I have just started using Resharper to analyze my code and there are many things it has suggested that I do and I am really pleased with its output as it is also teaching me a few better methods for doing things.

Obviously its suggestions can be ignored and one of these I would just like to get some feedback from the community on!

I have always preferred to use the 'this' qualifier for properties that belong to the current class (i.e this.Name). No real reason - I just seemed to adopt this many moons ago.

Resharper suggests that this is redundant and maybe I should remove it.

What do you think?

Community
  • 1
  • 1
EzaBlade
  • 657
  • 1
  • 9
  • 23
  • 1
    There are definitely some duplicates of that: http://stackoverflow.com/questions/725770/should-the-java-this-keyword-be-used-when-it-is-optional and http://stackoverflow.com/questions/3920279/should-i-use-this-keyword-in-oop-programing-where-its-possible-to-skip-it-an – Dyppl May 04 '11 at 14:46
  • 3
    You know those dreams where you're in class or at work and you're not wearing any clothes? That's how member property or method references without `this.` make me feel. – Anthony Pegram May 04 '11 at 14:49
  • Considering the analogy, wearing clothes in this case would be a unnecessary, borderline neurotic behavior. – Timothy Groote May 10 '11 at 10:15

7 Answers7

18

Preferably, i use this only to prevent ambiguity between (possibly) a property and a function parameter

public class thing
{

   private string name;

   public thing(string name)
   {
       this.name = name; // will set private string name to param string name
   }

}

If you are already working in the context of a certain class, it's not so hard to keep this in mind, and i do not need to be reminded of the fact that I am addressing a local variable every time i address one.

So I think ReSharper's right on this one.

Timothy Groote
  • 8,614
  • 26
  • 52
13

One of the important things to remember is that this is removed by the compiler and so it is purely a matter of 'what looks good to you and those with whom you share code?'. It will affect performance not a whit.

Cos Callis
  • 5,051
  • 3
  • 30
  • 57
  • Thanks - that's my main concern - whether my preference for doing something is less optimized for performance. – EzaBlade Jun 15 '11 at 11:35
12

I find it redundant particularly with a well defined coding standard:

Name // Property
_name // Member field
name // local variable

Using this.<whatever> just seems to be more work.

Tejs
  • 40,736
  • 10
  • 68
  • 86
9

I personally think that it's a good practice to use this keyword because it's clearly marks does prorerty/method/etc belongs to object instance or not. This is especially useful then private members are named so that local variables are indistinguishable from private members:

/// some pretty long method
/// ...
frameCount += 1; // is it private memeber or some local defined above?

Needless to say it's probably better not to use it at all than use inconsistenly.

However I find that constant usage of this makes code look "noisy" with this being unuseful distractor. IMO it's better to use special _ prefix naming for private members:

/// some pretty long method
/// ...
_frameCount += 1; // it's clearly private memeber!

it's also much faster to type.

Petr Abdulin
  • 33,883
  • 9
  • 62
  • 96
  • I agree with `_fieldName` as private. This is a known convention in other languages like python. – Daniel Jan 20 '19 at 22:30
8

One advantage of using this. is intellisense. It narrows the list of things you could pick faster.

n8wrl
  • 19,439
  • 4
  • 63
  • 103
3

It is technically redundant, but StyleCop (if you're being picky and using the full set of Microsoft coding standards) says you should use it.

I do, lots of people don't, so I guess it's down to personal preference or the coding standard at your employer.

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
2

In this case its a preference - so you prefer to use it, tell resharper that and it will stop complaining.

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155