Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make using `this` mandatory in my code?

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.

like image 543
T.J. Crowder Avatar asked Dec 05 '25 04:12

T.J. Crowder


1 Answers

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?

like image 81
Ash Burlaczenko Avatar answered Dec 07 '25 16:12

Ash Burlaczenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!