Take the 2-minute tour ×

Is it a simple case of just never using the this.XYZ construct?

share|improve this question

5 Answers 5

up vote 6 down vote accepted

It's only considered poor style if it violates your style guidelines. Sometimes using this is necessary to qualify a member variable over a local variable:

public MyType(int arg)
    this.arg = arg;

This problem can also be mitigated with style guidelines. For example, prefix members with "_":

public MyType(int arg)
    _arg = arg;
share|improve this answer

I wouldn't say it's poor style - but it's not particularly idiomatic.

My almost sole use of this.foo is when copying parameters into fields:

public Person (string name, string occupation)
   this.name = name;
   this.occupation = occupation;
share|improve this answer
You don't preface your fields with underscores? –  Will Nov 21 '08 at 15:53
Nope. I never have in my personal code. I've worked at places which do, and I've worked at places which don't. I don't care much any more. What scares me is that I just subconsciously used "brace at end of line" which is the Google style rather than my personal style. Editing... –  Jon Skeet Nov 21 '08 at 15:56
in general, (except for in initializers - cctors) I always set the public properties, not the private fields, in constructors... so the ctor parameter is lowerCase, and the property is UpperCase... (no need for this.) Name = name; –  Charles Bretana Nov 21 '08 at 16:07
Charles: That doesn't work when the field is readonly so the property has no setter... –  Jon Skeet Nov 21 '08 at 16:12

I've never heard of it being a style issue. I used to do it all the time to get intellisense, but then I started using ctrl-j, then I just found myself remembering my object's properties without having to use a crutch.

Probably because my objects have become less complex as I gain more experience...

share|improve this answer

I always use this. for global variable. This way, I can clearly know that I am using a global variable without having to use prefix like "_".

share|improve this answer
Yes, because it's so much easier to prefix with "this." than with "_" ;) –  Kent Boogaart Nov 21 '08 at 15:56
well, it's clearer in my opinion. And the Intellisense is there... it's part of the Framework.. so why not. –  Patrick Desjardins Nov 21 '08 at 15:57
I have to add that if you do it all the time and not only for parameter passing to global than it's become easy to follow a code because it's always the same. not "sometime" it's used for global and some time not... but it's just my opinion. –  Patrick Desjardins Nov 21 '08 at 15:58
Yep, makes sense. I haven't yet worked on a project that required prefixing with "this." so don't have any experience having to do so on a regular basis. Was just poking fun... –  Kent Boogaart Nov 21 '08 at 16:05
No problem, I think it's really just an opinion and they aren't any good answer. –  Patrick Desjardins Nov 21 '08 at 16:06

The MS tool StyleCop insists on the this.XYZ (or should that be this.Xyz) variant when analysing source code.

share|improve this answer
That's just based on its default rules. You can customize the rules to suit your organization or team. –  Kent Boogaart Nov 21 '08 at 16:06

Your Answer


