CA1501: Avoid excessive inheritance

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName AvoidExcessiveInheritance
CheckId CA1501
Category Microsoft.Maintainability
Breaking Change Breaking

Cause

A type is more than four levels deep in its inheritance hierarchy.

Rule Description

Deeply nested type hierarchies can be difficult to follow, understand, and maintain. This rule limits analysis to hierarchies in the same module.

How to Fix Violations

To fix a violation of this rule, derive the type from a base type that is less deep in the inheritance hierarchy or eliminate some of the intermediate base types.

When to Suppress Warnings

It is safe to suppress a warning from this rule. However, the code might be more difficult to maintain. Note that, depending on the visibility of base types, resolving violations of this rule might create breaking changes. For example, removing public base types is a breaking change.

Example

The following example shows a type that violates the rule.

using System;

namespace MaintainabilityLibrary
{
   class BaseClass {}
   class FirstDerivedClass : BaseClass {}
   class SecondDerivedClass : FirstDerivedClass {}
   class ThirdDerivedClass : SecondDerivedClass {}
   class FourthDerivedClass : ThirdDerivedClass {}

   // This class violates the rule.
   class FifthDerivedClass : FourthDerivedClass {}
}
Imports System

Namespace MaintainabilityLibrary

   Class BaseClass
   End Class

   Class FirstDerivedClass
      Inherits BaseClass
   End Class

   Class SecondDerivedClass
      Inherits FirstDerivedClass
   End Class

   Class ThirdDerivedClass
      Inherits SecondDerivedClass
   End Class

   Class FourthDerivedClass
      Inherits ThirdDerivedClass
   End Class

   ' This class violates the rule.
   Class FifthDerivedClass
      Inherits FourthDerivedClass
   End Class

End Namespace