简体   繁体   English

C#StyleCop - 对于像当前类成员这样的基类成员使用“this。”前缀?

[英]C# StyleCop - Using “this.” prefix for base class members like current class members or not?

StyleCop has a rule about using "this." StyleCop有一个关于使用“this”的规则。 prefix to calling class members (SA1101). 调用类成员的前缀(SA1101)。

Is this rule holds true about a member (for example a method) of a class which is inherited from its base class. 这个规则是否适用于从其基类继承的类的成员(例如方法)。

Example: 例:

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. 我知道这只是为了更好的可读性。

The documentation for StyleCop Rule SA1101 actually mentions this: 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”为前缀的基类 ,就会违反此规则。

(emphasis added by myself). (重点由我自己添加)。 So yes, the rule requires this. 所以是的,规则需要this. on every access to an instance member, irrespective of whether that member is in the local class or inherited from a base class. 每次访问实例成员时,无论该成员是在本地类中还是从基类继承。

If you think about the rules for object inheritance, even though F1() is actually declared on BaseClass it is inherited by ChildClass so it is valid to call it as this.F1() . 如果考虑对象继承的规则,即使实际在BaseClass声明了F1() ,它也会被ChildClass继承,因此将其称为this.F1()是有效的。 This is what StyleCop is telling you to do. 这就是StyleCop告诉你的事情。 By prefixing the call with this , it becomes unambiguous that you are calling the F1() instance method of the current runtime instance of the class. 通过this调用前缀,可以明确地调用类的当前运行时实例的F1() 实例方法。

In fact, calling it as F1() or this.F1() are actually synonymous, but the meaning/intent becomes clearer when using the this prefix. 实际上,将其称为F1()this.F1()实际上是同义词,但使用this前缀时,含义/意图会变得更清晰。

You should not use the base prefix here at all (even though it will compile) because F1() is not virtual and being overridden in ChildClass . 你根本不应该在这里使用base前缀(即使它会编译)因为F1()不是虚拟的并且在ChildClass被覆盖。 The only reason to use the base prefix is when you have overridden a virtual base class member and want to explicitly call that base class member from within the overriding member. 使用base前缀的唯一原因是,当您重写了虚拟基类成员并希望从覆盖成员中显式调用该基类成员时。 If you did actually use the base prefix without F1() being virtual everything would actually work until you made F1() virtual and added an override in ChildClass . 如果你确实使用了base前缀而没有F1()是虚拟的, 那么在你创建F1()虚拟并在ChildClass添加覆盖之前,一切都会有效。 At that point, any calls to base.F1() would continue calling BaseClass.F1() and not the new override in ChildClass . 此时,对base.F1()任何调用都将继续调用BaseClass.F1()而不是ChildClass的新覆盖。

I believe that is correct since the rule holds for all methods regardless of whether they are defined on the base or not. 我认为这是正确的,因为规则适用于所有方法,无论它们是否在基础上定义。 Personally I am not a huge fan of this rule so I just disable it. 就个人而言,我不是这个规则的忠实粉丝所以我只是禁用它。

I like to use base. 我喜欢用base。 base.F1() for your case. base.F1()为您的情况。 That prevents accidently referencing a local variable, and is a visual reminder of where the member came from. 这可以防止意外地引用局部变量,并且可以直观地提醒成员来自何处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM