Home > Code Analysis > CodeRush Code Issues for interfaces

CodeRush Code Issues for interfaces

October 24th, 2011 Leave a comment Go to comments

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

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:

CodeRush - Cannot create an instance of interface

Instead, we can create an object instance that implementes the interface:

CodeRush - Cannot create an instance of interface valid code

Interface cannot contain constructors

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:

CodeRush - Interface cannot contain constructors

Interface member cannot have a definition

As I mentioned before, interfaces do not provide any functionalities. That is why interface members cannot have a definition. If you provide a definition of a member, an error is shown:

CodeRush Interface member cannot have a definition

Interface events cannot have add or remove accessors

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. If you ignore this rule, an error will be shown in the code editor:

CodeRush - Interface events cannot have add or remove accessors

There’s also an additional code issue related to interfaces called Interface expected. It shows an error when the structure is derived from a non-interface type, because structures can only inherit from other interfaces by their nature and guidelines:

CodeRush Code Issues - Interface Expected

—–
Products: CodeRush Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Oct/17/2011
ID: C139

Similar Posts:

  1. No comments yet. Be the first and leave a comment!
  1. October 25th, 2011 at 04:15 | #1