StyleCop 有一条关于使用“this”的规则。调用类成员的前缀 (SA1101)。
这条规则是否适用于从其基类继承的类的成员(例如方法)。
例子:
class BaseClass
{
protected void F1()
{
...
}
}
class ChildClass : BaseClass
{
protected void F2()
{
...
}
protected void F3()
{
this.F2(); // This is correct acording to SA1101
// F1 is a member of base class and if I dont put this prefix, stylecop will not show any message.
this.F1(); // Is this correct?
F1(); // Or this?
}
}
我知道这只是为了更好的可读性。
最佳答案
documentation for StyleCop Rule SA1101 实际上提到了这一点:
A violation of this rule occurs whenever the code contains a call to an instance member of the local class or a base class which is not prefixed with ‘this.’.
(重点是我自己加的)。所以是的,规则要求在每次访问实例成员时使用 this.
,无论该成员是在本地类中还是从基类继承。
关于C# StyleCop - 是否对当前类成员等基类成员使用 "this."前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3663215/