3

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

I'm writing some of my first C# code, and I notice that this.foo is only necessary when there is a local name foo different from the this.foo. I have so far been inconsistent as to whether or not I use this.

Is it preferred to always use this or is it preferred to only use this when necessary?

Community
  • 1
  • 1
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
  • 21
    Enter holy war..... – Russell McClure Feb 08 '11 at 21:03
  • This has been asked a few times: http://www.google.com/search?q=c%23+this%20site:stackoverflow.com#sclient=psy&hl=en&safe=active&q=c%23+this+keyword+required+site:stackoverflow.com&aq=f&aqi=&aql=&oq=&pbx=1&fp=5b2df7f8ff10eaf9 – Andrew Hare Feb 08 '11 at 21:04
  • It's a style thing. If you're working on your own, do whatever you'd like. If you're part of a team, follow whatever convention has been established. – Andrew Anderson Feb 08 '11 at 21:05
  • Voting to close. I'm sorry but there is no correct answer to this. Some prefer to use it, some don't. Neither is more right. – Lasse V. Karlsen Feb 08 '11 at 21:06
  • possible duplicate of [When do you use the "this" keyword?](http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword) , qqv [this](http://stackoverflow.com/questions/3663215/c-stylecop-using-this-prefix-for-base-class-members-like-current-class-memb) [this](http://stackoverflow.com/questions/2841807/when-not-to-use-this-keyword) etc etc – AakashM Feb 08 '11 at 21:06
  • 12
    I should have known `this` was already asked. – Robert Harvey Feb 08 '11 at 21:08
  • FWIW, I searched SO before writing this question. Thanks. – Jacob Marble Feb 08 '11 at 21:23

7 Answers7

5

I only use this when it is necessary. Most of the time I don't have the issue of having to differentiate between a local and class variable, so I just don't bother unless the need comes up

khr055
  • 28,690
  • 16
  • 36
  • 48
1

I always type 'this' because it allows me to use Visual Studio's Intellisense. I don't think that there is any meaningful stylistic objection to using 'this' for each reference to an object property or method.

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
1

It's really a matter of style - some people like to use this all the time, since it makes it clear when you're accessing a member variable instead of method local, some people use warts on the name for the same purpose, and some people don't use either.

Regardless, it's probably a bad idea to use the same name for a method local as for a member variable - the only time I'd consider it legitimate is when setting those members in the constructor or other initialization routine.

Anon.
  • 58,739
  • 8
  • 81
  • 86
0

I do prefer it because of clarity. But also because we follow StyleCop's rules (almost all of them :) ).

Why does StyleCop recommend prefixing method or property calls with "this"?

Community
  • 1
  • 1
Russell McClure
  • 4,821
  • 1
  • 22
  • 22
0

It's based entirely on your preference. I personally like to use it all the time to emphasize that it's a class variable, but that's just me.

Peter C
  • 6,219
  • 1
  • 25
  • 37
0

I use this when I want to clarify my intent.

But generally, the Visual Studio IDE will clarify the member for you (with a popup) if you hover over the member in question. That reduces the need for this.

As Adam points out, typing this first will intellisense only the local members for you, instead of giving you the entire intellisense list.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

Just to give an answer that contrasts with the many "pro this" answers...

I almost never use this. I prefix all member variables with an underscore, and so I can easily tell local and member variables apart. I find using this all the time really pollutes the code and makes it far more noisy.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • 3
    I find added strange prefixes to member variables really pollutes the code and makes it far more noisy... – Peter C Feb 08 '11 at 21:08
  • Plus, if you have a public member variable prefixed with an underscore, people who do lots of Python (or JavaScript) are gonna be pretty confused. :P – Peter C Feb 08 '11 at 21:10
  • I find a simple underscore prefix not to be distracting (far less distracting than "this." anyway), although I do agree prefixes like "m_" are hard to swallow. And you should never have a public member variable in C# (that's a major red flag in my book, use properties please). In JS, Python, etc different languages call for different styles. – Matt Greer Feb 08 '11 at 21:33
  • Interesting - when being used purely for readability or disambiguation, I consider both underscores and "this." to be similarly poluting. This is a point on which I happen to disagree with Style Cop - I don't use them, but I don't think it's worth the Holy War it's become. Do what works for you and your team. – Chris Rogers Jun 06 '11 at 22:21