Avoid excessive inheritance
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Avoid excessive inheritance







Breaking Change


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

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

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

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.

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 {}
© 2016 Microsoft