C#StyleCop - 使用“this”。前缀的基类成员像当前类成员? [英] C# StyleCop - Using "this." prefix for base class members like current class members or not?

查看:211
本文介绍了C#StyleCop - 使用“this”。前缀的基类成员像当前类成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StyleCop有关于使用this的规则。前缀到调用类成员(SA1101)。

StyleCop has a rule about using "this." prefix to calling class members (SA1101).

这个规则适用于继承自其基类的类的成员(例如方法)。

Is this rule holds true about a member (for example a method) of a class which is inherited from its base class.

例如:

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?
    }
}



我知道这只是为了更好的可读性。 p>

I know this is just for better readability.

推荐答案

StyleCop规则SA1101的文档 实际上提到了这一点:

The documentation for StyleCop Rule SA1101 actually mentions this:


只要代码包含对本地类的实例成员或不带前缀this的基类的调用,就会出现该规则。

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.’.

(强调由我自己添加)。所以是的,这个规则在每次访问实例成员时都需要。 c无论成员是在本地类还是继承自基类。

(emphasis added by myself). So yes, the rule requires this. on every access to an instance member, irrespective of whether that member is in the local class or inherited from a base class.

这篇关于C#StyleCop - 使用“this”。前缀的基类成员像当前类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆