C# StyleCop - 使用“this”。基类成员的前缀是否像当前类成员一样?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StyleCop 规则 SA1101 的文档实际上提到了这一点:
(重点是我自己添加的)。所以,是的,规则要求每次访问实例成员时都需要
this.
,无论该成员是在本地类中还是从基类继承。The documentation for StyleCop Rule SA1101 actually mentions 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.如果您考虑一下对象继承的规则,即使
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 onBaseClass
it is inherited byChildClass
so it is valid to call it asthis.F1()
. This is what StyleCop is telling you to do. By prefixing the call withthis
, it becomes unambiguous that you are calling theF1()
instance method of the current runtime instance of the class.In fact, calling it as
F1()
orthis.F1()
are actually synonymous, but the meaning/intent becomes clearer when using thethis
prefix.You should not use the
base
prefix here at all (even though it will compile) becauseF1()
is not virtual and being overridden inChildClass
. The only reason to use thebase
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 thebase
prefix withoutF1()
being virtual everything would actually work until you madeF1()
virtual and added an override inChildClass
. At that point, any calls tobase.F1()
would continue callingBaseClass.F1()
and not the new override inChildClass
.我相信这是正确的,因为该规则适用于所有方法,无论它们是否在基础上定义。就我个人而言,我不太喜欢这条规则,所以我只是禁用它。
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.
我喜欢用基地。 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.