Thursday, March 3, 2011

Do you use the "this" operator in C#?

Duplicate post, see: When do you use the "this" keyword?


On almost every project I worked the "This" operator is used, when i start developing i was told that it is a good practice. is this really necessary does it gives you more readability?

From stackoverflow
  • Tools like Resharper have a built in hint saying "redundant qualifier," but I disagree with it and quickly disable the rule.

    I always use the this qualifier because it lets me know at a glance whether or not the reference is a property/field, or a static class ref for example:

    public class MyClass {
        public int Foo { get; set; }
    }
    
    public MyClass MyRef { get; }
    

    or

    public static class MyRef {
       public static int Foo { get; set; }
    }
    

    so:

    void method() {
       MyRef.Foo = 4; // might be either
    }
    
    void method() {
       this.MyRef.Foo = 4; // definitely property/field
    }
    

    Just my 2c.

    -Oisin

    usr : what a contrived example. why do you optimize for the infrequent case?

0 comments:

Post a Comment