Tutorial :Should I be using `this` by default? [duplicate]


We all know that this refers to the actual instance of a class...but it seems to me that most people are not using it. So, should I be using it?

My thoughts about it are as follows: Since this refers to the actual instance of a class, it should be used to access any member of that class.

  public void(String newValue) {          this.privateVariable = newValue;    }  

This should guarantee that the value is not assigned to an object with the same name within the scope (which could also end up as undefined behavior, anyway). But, what if an underscore is used to separate private from non-private fields:

  public voud(String newValue) {          _privateVariable = newValue;    }  

this seems in that case a little redundant and unnecessary.

So, are there good reasons to use or not use this? Or am I just racking my brain about nothing?


Personally I only use it when it's necessary, e.g.

public MyClass(string name)  {      this.name = name;  }  

I prefer that to renaming the parameter or field from the most natural name. I don't use this where it's otherwise unnecessary though. Having said that, there's absolutely no harm in doing so - and if you feel that it makes your code clearer, that's a very good reason to use it.


Nobody mentionned that you must use this if the requirement is to make a StyleCop compliant code and do not violate the readability rule SA1101. According to the documentation,

A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’.

Also, in my opinion, using this keyword must be encouraged even for non-StyleCop-compliant source code, since specifying this increases readability and reduces the risk of mixing between local (method-scope) and class (object-scope) variables.


You should use the naming-convention approach. (The one with the undescore.)

There are dangers when changing code with the "this" approach if "this" is forgotten.

Why? If MyMethod in the followin class is later changed to include a local variable called "something" the compiler still compiles, while the code no longer does whats expected.

class MyClass  {      int something = 10;        void MyMethod()      {          //...lots'a code here...            something = 20;  //Which var is something pointing at?      }  }  

Changing it to the following will introduce a bug...

class MyClass  {      int something = 10;        void MyMethod()      {          int something = 0;          //...lots'a code here...            something = 20;  //Which var is something pointing at?      }  }  

So unless ALL developers working on your team, sharing code, always will remember "this"; don't go that way!


Personally I make sure that I never have name collisions between class members and locally declared variables. This means I never use this. However its all about readability really. If you think a "this" will make your code more intuitively understandable then use it, if not then don't.


I like using this. as it brings up intellisene which means I can quite often type a few letters of the method, variable and auto complete - makes writing code faster!


My two cents: Underscores are ugly!

=> fields are just camelCase

=> confusion with camelCase local variables is possible

=> always qualify with this


I used to use this all the time - probably a habit from pythons explicit self argument. I like to be reminded of when I use instance state.

Using Resharper from JetBrains made me brake that habit, since it would always flag this as redundant.


I use no special indicator for fields vs. locals (they are all ugly). I find that if it is getting harder to track which are which I'm better served by breaking up methods to be shorter with less local state. (The one exception is where the natural name of a parameter matches a fieldâ€"most often occurs with constructorsâ€"then using this. to disambiguate.)

Edit: Added the one exception (thanks to other answers for reminding me).


It is subjective, but I prefer not to have it unless it provides unambiguity.

Using unnecessary this is like saying 'Create New Person'.

You choose :)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »