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

Linq to Sql Sum with no records

linq-to-sql error-handling sum

I am creating a method that collects accumulated totals throughout the month. The problem is that there may not be charges for some of the items in a given month so no rows would be returned.

How do I find out whether enabling gcServer config is taking effect?

asp.net garbage-collection

I'm supporting a ASP.NET v2.0 app installed on a Windows 2003 SP3 Enterprise on a quad core 8G machine running on .NET 2.0 SP1.