C# StyleCop - 是否对当前类成员等基类成员使用 "this."前缀?

标签 c# coding-style this stylecop

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/

相关文章:

python - 如何在 Python 中正确声明实例字段

C++ 在条件中依赖隐式转换为 bool?

coding-style - 编码风格 : lock/unlock internal or external?

javascript - 购物车挑战 JavaScript 逻辑。有不止一种方法可以解决吗?

javascript - TypeError : this. attr 不是类悬停上的函数

javascript - 'that' 传递时与 'this' 不一样,为什么?

c# - XDocument 的简单搜索

c# - 具有上下文处理的异步方法

c# - DataReader - 硬编码序数?

c# - string.Split 方法的运行时间