We recommend using Visual Studio 2017

CA1053: Static holder types should not have constructors

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CA1053: Static holder types should not have constructors.

TypeName|StaticHolderTypesShouldNotHaveConstructors|
|CheckId|CA1053|
|Category|Microsoft.Design|
|Breaking Change|Breaking|

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

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.

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

System_CAPS_ICON_note.jpg 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.

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

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() {}
   }
}

Show: