10

In C# using "this."

WHY? It doesn't help with readability. It just clutters up the code and adds nothing.

It should never be a required prefix.

What makes it worse is, Visual Studio greys it out because its not required!

Then there is stylecop. This got enabled on our project which generated 3000 build errors because of missing "this."

Comments
  • 5
    I just declared a global variable and a local one with the same name and used this.variableName to reference the global one, as you should in this case...and got no grey out...
    VS 2015 Community Edition.
  • 0
    @VenomCLC yeah which is the only valid use case.

    I personally prefer underscore prefix to identify class fields.
  • 0
    @BungleFunk well....yes....I mean you're not gonna use this.globalVar to identify a global variable if you don't have a local one with the same name, that's where it greys out, if stylecop tells you to put "this." when not needed it means yours is badly configured.
  • 0
    @azuredivay I have never needed "this" while creating commands in a view model so I don't understand where you are coming from.
  • 2
    @VenomCLC The stylecop rules were changed by another dev which caused the 3000 build errors :(
  • 0
    Resharper cleanup
  • 0
    Completely agree, probably the biggest reason why StyleCop is not enabled in our project.
  • 0
    @nickRm Rather then remove the rule they set all stylecop errors as warning so now we have 3000 warnings.

    Kinda defeats the purpose.
  • 1
    I think the only time I use "this" is when assigning constructor parameters to local, if they have the same name - or using an extension for that interface / class (rare)
Add Comment