C# StyleCop - 使用“this”。基类成员的前缀是否像当前类成员一样?

发布于 2024-09-18 11:32:17 字数 575 浏览 12 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海少女心 2024-09-25 11:32:17

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

只要代码包含对本地类或基类的实例成员的调用,且该实例成员不以“this”为前缀,就会违反此规则。


(重点是我自己添加的)。所以,是的,规则要求每次访问实例成员时都需要 this.,无论该成员是在本地类中还是从基类继承。

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.

寻梦旅人 2024-09-25 11:32:17

如果您考虑一下对象继承的规则,即使 F1() 实际上是在 BaseClass 上声明的,它也是由 ChildClass 继承的,因此它是有效的将其称为 this.F1()。这就是 StyleCop 告诉你要做的。通过在调用前加上 this 前缀,可以清楚地看出您正在调用该类当前运行时实例的 F1() instance 方法。

事实上,将其称为 F1()this.F1() 实际上是同义词,但是当使用 this 时,含义/意图会变得更清晰> 前缀。

您根本不应该在此处使用 base 前缀(即使它会编译),因为 F1() 不是虚拟的并且在 ChildClass 中被覆盖。使用 base 前缀的唯一原因是当您重写了虚拟基类成员并希望从重写成员中显式调用该基类成员时。如果您确实使用了 base 前缀,而没有将 F1() 虚拟化,那么在您创建 F1() 之前,所有内容都会实际工作。 code> virtual 并在 ChildClass 中添加了覆盖。此时,对 base.F1() 的任何调用都将继续调用 BaseClass.F1(),而不是 ChildClass 中的新覆盖。

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(). This is what StyleCop is telling you to do. 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.

In fact, calling it as F1() or this.F1() are actually synonymous, but the meaning/intent becomes clearer when using the this prefix.

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. 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. 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. At that point, any calls to base.F1() would continue calling BaseClass.F1() and not the new override in ChildClass.

妄司 2024-09-25 11:32:17

我相信这是正确的,因为该规则适用于所有方法,无论它们是否在基础上定义。就我个人而言,我不太喜欢这条规则,所以我只是禁用它。

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.

静若繁花 2024-09-25 11:32:17

我喜欢用基地。 base.F1() 适合您的情况。这可以防止意外引用局部变量,并且可以直观地提醒成员来自哪里。

I like to use base. base.F1() for your case. That prevents accidently referencing a local variable, and is a visual reminder of where the member came from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文