Wednesday, March 16, 2011

In C#, is accessing the current object's properties through this.XYZ considered poor style compared to just XYZ

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

From stackoverflow
  • 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

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

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

    Kent Boogaart : Yes, because it's so much easier to prefix with "this." than with "_" ;)
    Daok : 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.
    Kent Boogaart : 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...
    Daok : No problem, I think it's really just an opinion and they aren't any good answer.
  • The MS tool StyleCop insists on the this.XYZ (or should that be this.Xyz) variant when analysing source code.

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

0 comments:

Post a Comment