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?

Answers


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


Need Your Help

Vector normalization

python math

The formula for half vector is (Hv) = (Lv + Vv) / |Lv+Vv|, where Lv is light vector, and Vv is view vector.

Remove n chars from end of String

java string integer

Can I somehow remove n chars from the end of a String ?