CA1040: Avoid empty interfaces

Item Value
RuleId CA1040
Category Design
Fix is breaking or non-breaking Breaking

Cause

The interface does not declare any members or implement two or more other interfaces.

By default, this rule only looks at externally visible interfaces, but this is configurable.

Rule description

Interfaces define members that provide a behavior or usage contract. The functionality that is described by the interface can be adopted by any type, regardless of where the type appears in the inheritance hierarchy. A type implements an interface by providing implementations for the members of the interface. An empty interface does not define any members. Therefore, it does not define a contract that can be implemented.

If your design includes empty interfaces that types are expected to implement, you are probably using an interface as a marker or a way to identify a group of types. If this identification will occur at run time, the correct way to accomplish this is to use a custom attribute. Use the presence or absence of the attribute, or the properties of the attribute, to identify the target types. If the identification must occur at compile time, then it is acceptable to use an empty interface.

How to fix violations

Remove the interface or add members to it. If the empty interface is being used to label a set of types, replace the interface with a custom attribute.

When to suppress warnings

It is safe to suppress a warning from this rule when the interface is used to identify a set of types at compile time.

Suppress a warning

There are various ways to suppress a code analysis warning, including disabling the rule for the project, using a preprocessor directive to disable it for a specific line of code, or by applying the SuppressMessageAttribute attribute. For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules, or for all rules in this category (Design). For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following example shows an empty interface.

// Violates rule
public interface IBadInterface
{
}
' Violates rule
Public Interface IBadInterface
End Interface