Home > Code Analysis > Code Issues specific to declaring types

Code Issues specific to declaring types

August 9th, 2012

The CSharp and Visual Basic programming language specifications have several restrictions on how type elements such as a class, structure, and delegate are declared in the code. Let’s review code issues that demonstrate an incorrect type declaration, and see the error before the compiler informs us about it when we build the code.

Cannot inherit from sealed type

Cause:

The whole point of sealed classes is that you are not supposed to inherit from them. That is why you are not allowed to create descendants of a sealed class.

Sample:

CodeRush Cannot inherit from sealed type

How to fix:

  • Remove the ‘sealed’ (‘NotInheritable’ in VB) keyword from the ancestor class if appropriate.
  • If you inherit the new class to extend it with additional members, or you do not have access to the source declaration of the sealed class, you can use the extensions methods instead:

CodeRush Cannot inherit from sealed type fix

Cannot inherit from special class System.ValueType

Cause:

The System.ValueType provides the base class for value types (structures). Because structures are implicitly sealed, they cannot be inherited. Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly.

Sample:

CodeRush Cannot inherit from special class System.ValueType

How to fix:

  • Inherit a descendant of a different non-sealed type rather than the ValueType type:

CodeRush Cannot inherit from special class System.ValueType

Generic class cannot derive from Attribute

Cause:

An attribute decorates a class when compiled, but a generic class does not receive its final type information until runtime. Since the attribute can affect compilation, it has to be completed at compile time, so a generic class can not inherit from the Attribute class.

Sample:

CodeRush Generic class cannot derive from Attribute

How to fix:

  • Remove the attribute from the current class declaration:

CodeRush Generic class cannot derive from Attribute Fix2

  • Do not mark the current class as generic:

CodeRush Generic class cannot derive from Attribute Fix1

Cannot create an instance of abstract class

Cause:

The abstract classes may contain abstract members that are incomplete and must be implemented in a derived class. The issue occurs because we have not overridden the abstract member to provide their implementation, so we can not instantiate an instance of an abstract class.

Sample:

CodeRush Cannot create instance of abstract class

How to fix:

  • Create a descendant implementation of the abstract class and instantiate it instead of the abstract class:

CodeRush Cannot create instance of abstract class fix

Sealed class cannot be abstract

Cause:

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. But, as we just learned, we can not derive from sealed classes, so a class can not be marked as sealed and abstract at once.

Sample:

CodeRush Sealed class cannot be abstract

How to fix:

  • Remove the ‘sealed’ modifier if a class can be used as an ancestor for derived classes:

CodeRush Sealed class cannot be abstract Fix2

  • Remove the ‘abstract’ modifier if a class does not contain abstract members:

CodeRush Sealed class cannot be abstract Fix1

Delegate cannot be marked static

Cause:

A delegate declaration is a type declaration that basically declares a method signature. That is why it doesn’t make sense to make it neither static nor an instance.

Sample:

CodeRush Delegate cannot be marked static

How to fix:

  • Remove the static keyword from a delegate declaration and add a static field instead, if required:

CodeRush Delegate cannot be marked static

See also the code issues for declaring variables and constants.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Aug/09/2012
ID: C172

Similar Posts: