CodeRush code issues for abstract members
In addition to CodeRush code issues for virtual members, there are similar code issues for abstract members. An abstract member is similar to a virtual member but with no implementation, in other words, it has no body. In contrast to virtual members, abstract members must be implemented in a derived class if one is inherited from the class that contains an abstract member.
An abstract member should be marked with an ‘abstract‘ keyword and declare an access modifier, return type, and member signature. A non-abstract derived class is required to provide its own implementation by overriding the abstract members from the base class.
Here are code issues that help you declare abstract members correctly without violating their declaration rules:
Abstract member cannot be declared in non-abstract class
Cause:
The class that holds an abstract member must be marked as abstract as well. If a class containing an abstract member is not marked as abstract, you will see the corresponding code issue highlighting of an error type.
Sample:
How to fix:
- Mark the class as abstract:
- Do not mark the member as abstract:
Abstract member cannot be private
Cause:
The visibility of an abstract member must be greater than ‘private‘, because it must be visible to derived classes. An abstract member with a ‘private‘ access modifier is highlighted with an error in the code editor.
Sample:
How to fix:
- Change the visibility of the abstract member:
Abstract member cannot be marked as sealed
Cause:
It is not allowed to declare an abstract member inside a class marked as sealed. Sealed classes cannot be inherited, so they cannot contain abstract members.
Sample:
How to fix:
- Do not mark an abstract member as ‘sealed’:
Abstract member cannot be marked as virtual
Cause:
An abstract member is not allowed to be marked as ‘virtual‘ and ‘abstract‘ at the same time – this is a violation of the language specification. Once the member is marked as an ‘abstract‘ and ‘virtual‘ is found, the corresponding error is shown.
Sample:
How to fix:
- Do not mark an abstract member as ‘virtual’:
Abstract member cannot declare a body
Cause:
By its nature, an abstract member should have no body. If you’ve started to code an implementation of an abstract member, you will see an error.
Sample:
How to fix:
- Remove the body of an abstract member:
- Do not mark the member as ‘abstract’:
Also, take a look at the code issues for overridden members, which override abstract members to learn more.
—– Products: CodeRush Pro Versions: 12.1 and up VS IDEs: any Updated: Nov/19/2012 ID: C123