CodeRush Code Issues for interfaces
An interface is a code structure that has no concrete members and similar to an abstract class. An interface can contain public members such as methods, events, properties and indexers, but these members do not provide any functionality. Instead, they define items that must be made concrete within all classes that implement this interface. This means that an interface can be used to define what a class must do, but not how it will achieve it.
Interfaces are declared using several declaration rules: interfaces cannot have fields or constants, interface members cannot have a definition, an interface instance cannot be created, etc. Most of these rules are covered by the CodeRush Code Issues technology that allows you to correctly declare an interface and its members while writing code.
Here they are:
Cannot create an instance of interface
Cause:
We cannot initialize an interfaces instance, because this is not allowed by the .NET Framework guidelines. If you try to create an interface interface, you will see an error.
Sample:
How to fix:
- Instead, we can create an object instance that implements the interface:
Interface cannot contain constructors
Cause:
An interface cannot have constructors, because you cannot create an interface. Should there be a constructor in an interface, an error message will be shown.
Sample:
How to fix:
- Remove the incorrect constructor.
Interface member cannot have a definition
Cause:
As I mentioned before, interfaces do not provide any functionality. That is why interface members cannot have a definition. If you provide a definition of a member, an error is shown.
Sample:
How to fix:
- Remove the body of the member:
Interface events cannot have add or remove accessors
Cause:
An event in an interface cannot have add or remove accessors. Interface events must be declared without add or remove accessors. This is a rule for interface events.
Sample:
How to fix:
- Remove the event accessors definition:
Interface expected
Cause:
This code issue shows an error when the structure is derived from a non-interface type, because structures can only inherit from other classes by their nature and guidelines.
Sample:
How to fix:
- Do not derive structure from a class: