Code Issues specific to declaring types
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:
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:
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:
How to fix:
- Inherit a descendant of a different non-sealed type rather than the ValueType type:
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:
How to fix:
- Remove the attribute from the current class declaration:
- Do not mark the current class as generic:
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:
How to fix:
- Create a descendant implementation of the abstract class and instantiate it instead of the abstract class:
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:
How to fix:
- Remove the ‘sealed’ modifier if a class can be used as an ancestor for derived classes:
- Remove the ‘abstract’ modifier if a class does not contain abstract members:
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:
How to fix:
- Remove the static keyword from a delegate declaration and add a static field instead, if required:
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