Navigation:  StyleCop 4.3 > StyleCop 4.3 Rules > Readability Rules >

SA1100: DoNotPrefixCallsWithBaseUnlessLocalImplementationExists

Previous pageReturn to chapter overviewNext page
StyleCop 4.3

Send comments on this topic.

SA1100: DoNotPrefixCallsWithBaseUnlessLocalImplementationExists
Glossary Item Box
TypeName

DoNotPrefixCallsWithBaseUnlessLocalImplementationExists

CheckId

SA1100

Category

Readability Rules

 

Cause

A call to a member from an inherited class begins with ‘base.’, and the local class does not contain an override or implementation of the member.

Rule Description

A violation of this rule occurs whenever the code contains a call to a member from the base class prefixed with ‘base.’, and there is no local implementation of the member. For example:

   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

To fix a violation of this rule, change the ‘base.’ prefix to ‘this.’.

 


© Microsoft Corporation. All Rights Reserved.