Send comments on this topic. |
|
DoNotPrefixCallsWithBaseUnlessLocalImplementationExists |
|
CheckId |
SA1100 |
Category |
Readability Rules |
Cause
Rule Description
string name = base.JoinName("John", "Doe");
This rule is in place to prevent a potential source of bugs. Consider a base class which contains the following virtual method:
public virtual string JoinName(string first, string last)
{
}
Another class inherits from this base class but does not provide a local override of this method. Somewhere within this class, the base class method is called using base.JoinName(...). This works as expected. At a later date, someone adds a local override of this method to the class:
public override string JoinName(string first, string last)
{
return “Bob”;
}
At this point, the local call to base.JoinName(...) most likely introduces a bug into the code. This call will always call the base class method and will cause the local override to be ignored.
For this reason, calls to members from a base class should not begin with ‘base.’, unless a local override is implemented, and the developer wants to specifically call the base class member. When there is no local override of the base class member, the call should be prefixed with 'this.' rather than 'base.'.
How to Fix Violations
© Microsoft Corporation. All Rights Reserved.