views:

173

answers:

5

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

+5  A: 

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;
}

HTH, Kent

Kent Boogaart
+1  A: 

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;
}
Jon Skeet
You don't preface your fields with underscores?
Will
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
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
Charles: That doesn't work when the field is readonly so the property has no setter...
Jon Skeet
+1  A: 

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...

Will
+1  A: 

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 "_".

Daok
Yes, because it's so much easier to prefix with "this." than with "_" ;)
Kent Boogaart
well, it's clearer in my opinion. And the Intellisense is there... it's part of the Framework.. so why not.
Daok
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.
Daok
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
No problem, I think it's really just an opinion and they aren't any good answer.
Daok
+2  A: 

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

Peter Rennie
That's just based on its default rules. You can customize the rules to suit your organization or team.
Kent Boogaart