C#了StyleCop - 使用"这"前缀基类成员像当前类成员与否? [英] C# StyleCop - Using "this." prefix for base class members like current class members or not?

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

问题描述

了StyleCop有关于使用规则这一点。前缀调用类成员(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?
    }
}



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

I know this is just for better readability.

推荐答案

对了StyleCop规则SA1101文档 居然提到了这一点:

The documentation for StyleCop Rule SA1101 actually mentions 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.’.

(强调我自己加的)。所以,是的,规则要求这一点。每访问一个实例成员,不管该成员是否在本地类或从基类继承。

(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 - 使用"这"前缀基类成员像当前类成员与否?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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