All rules
CA1058Design Enabled by default: No

Types should not extend certain base types

Types should not extend certain base types

Microsoft docs

Description

Exceptions should derive from System.Exception or one of its subclasses in the System namespace.

Do not create a subclass of System.Xml.XmlDocument if you want to create an XML view of an underlying object model or data source.

### Non-generic collections

Use and/or extend generic collections whenever possible. Do not extend non-generic collections in your code, unless you shipped it previously.

Examples of Incorrect Usage

Examples of Correct Usage

Cause

A type extends one of the following base types:

  • System.ApplicationException
  • System.Xml.XmlDocument
  • System.Collections.CollectionBase
  • System.Collections.DictionaryBase
  • System.Collections.Queue
  • System.Collections.ReadOnlyCollectionBase
  • System.Collections.SortedList
  • System.Collections.Stack

By default, this rule only looks at externally visible types, but this is configurable.

How to fix violations

To fix a violation of this rule, derive the type from a different base type or a generic collection.

Example

public class MyCollection : CollectionBase
{
}

public class MyReadOnlyCollection : ReadOnlyCollectionBase
{
}

When to suppress

Do not suppress a warning from this rule for violations about System.ApplicationException. It is safe to suppress a warning from this rule for violations about System.Xml.XmlDocument. It is safe to suppress a warning about a non-generic collection if the code was released previously.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0