Home > Code Analysis > CodeRush Code Issues for interfaces

CodeRush Code Issues for interfaces

October 24th, 2011

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:

CodeRush - Cannot create an instance of interface

How to fix:

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

CodeRush - Cannot create an instance of interface valid code

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:

CodeRush - Interface cannot contain constructors

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:

CodeRush Interface member cannot have a definition

How to fix:

  • Remove the body of the member:

CodeRush Interface Member Cannot Have A Definition Fix

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:

CodeRush - Interface events cannot have add or remove accessors

How to fix:

  • Remove the event accessors definition:

CodeRush Interface Events Cannot Have Add Or Remove Accessors Fix

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:

CodeRush Code Issues - Interface Expected

How to fix:

  • Do not derive structure from a class:

CodeRush Interface Expected Fix

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/19/2012
ID: C139

Similar Posts:

  1. No comments yet. Be the first and leave a comment!