3

Is there a way to tell Visual Studio to make qualifying members of the current instance with this. mandatory? I prefer the clarity.

Details:

In C#, if you have instance members (fields, properties, methods, etc.), normally you can refer to them both with and without this, e.g.:

class Foo
{
    private int bar;

    Foo()
    {
        // These two lines are both valid, and both do exactly the same thing
        bar = 42;
        this.bar = 42;
    }
}

In my coding style, I don't want this to optional, I want the first line above to cause at least a warning. Obviously this is just for my own purposes, I'm not trying to mark the resulting assembly in some way that requires people using it to also do this, this is purely a personal style thing.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875

2 Answers2

6

You could use programs such as Resharper (using the setting described here) and StyleCop (using this rule).

They allow you to define your own coding styles which cause validation warning in your code with suggestions on who to resolve them.

Here is a question on free alternatives to resharper.

What are some alternatives to resharper?

Community
  • 1
  • 1
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
  • When stylecop is installed it should make this rule mandatory – The Unculled Badger Dec 09 '12 at 10:17
  • 1
    ReSharper has the option to force "this." qualifier. See [Let Resharper force this keyword on fields](http://stackoverflow.com/questions/4087361/let-resharper-force-this-keyword-on-fields) – comecme Dec 09 '12 at 10:21
  • Both of them have this, cool. (Updated the answer.) Would prefer it if VS.Net itself had something, but so far it's not seeming like it. – T.J. Crowder Dec 09 '12 at 10:44
  • Do you also enforce "this" on properties and methods? If not: Why? If so: Doesn't it bother you cluttering your code with "this" all over the place? – JustAnotherUser Dec 09 '12 at 11:50
1

StyleCop seems to have this rule switched on by default.

Yan Yankowski
  • 454
  • 4
  • 7