CA1053: Static holder types should not have constructors

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

TypeName StaticHolderTypesShouldNotHaveConstructors
CheckId CA1053
Category Microsoft.Design
Breaking Change Breaking

Cause

A public or nested public type declares only static members and has a public or protected default constructor.

Rule Description

The constructor is unnecessary because calling static members does not require an instance of the type. Also, because the type does not have non-static members, creating an instance does not provide access to any of the type's members.

How to Fix Violations

To fix a violation of this rule, remove the default constructor or make it private.

Note

Some compilers automatically create a public default constructor if the type does not define any constructors. If this is the case with your type, add a private default constructor to eliminate the violation.

When to Suppress Warnings

Do not suppress a warning from this rule. The presence of the constructor suggests that the type is not a static type.

Example

The following example shows a type that violates this rule. Notice that there is no default constructor in the source code. When this code is compiled into an assembly, the C# compiler will insert a default constructor, which will violate this rule. To correct this, declare a private constructor.

using System;

namespace DesignLibrary
{
   public class NoInstancesNeeded
   {
      // Violates rule: StaticHolderTypesShouldNotHaveConstructors.
      // Uncomment the following line to correct the violation.
      // private NoInstancesNeeded() {}

      public static void Method1() {}
      public static void Method2() {}
   }
}