Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is there a way to enforce use of "this/base" qualification in instance members?

Tags:

c#

stylecop

this

Sometimes it can be confusing reading code in instance members that refers to other instance members of the same class (or a base class):

public void MyMethod()
{
    Where = did + AllTheseWeirdThings(GetDeclared()); // ?
}

Having a coding standard something like "prefix all private/protected members with "_" doesn't help, because instance members can still refer to public members.

It would be much better to read this:

public void MyMethod()
{
    this.Where = this.did + base.AllTheseWeirdThings(this.GetDeclared()); // ?
}

Is there a way to enforce this, either with compiler options, StyleCop, or something similar?

like image 714
Bruce Johnston Avatar asked Dec 29 '22 08:12

Bruce Johnston


1 Answers

There's no compiler option that enforces your rule.

However, a cursory Google search brings up this StyleCop rule: http://www.thewayithink.co.uk/stylecop/sa1101.htm

ReSharper has a similar option.

like image 93
Tim Robinson Avatar answered Jan 04 '23 23:01

Tim Robinson