Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3453

Classes should not have only "private" constructors

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      This class can't be instantiated; make [at least one of its constructors|its constructor] "public".
    • Highlighting:
      Hide

      class name

      Show
      class name
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C#, VB.Net
    • Irrelevant for Languages:
      Python
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • ReSharper:
      ClassCannotBeInstantiated

      Description

      A class with only private constructors can't be instantiated, thus, it seems to be pointless code.

      Noncompliant Code Example

      public class MyClass // Noncompliant
      {
        private MyClass() { ... }
      }
      

      Compliant Solution

      public class MyClass 
      {
        public MyClass() { ... }
      }
      

      Exceptions

      Classes that themselves access their private constructors (singletons or smart enums) are ignored. Classes with only static members are also ignored because they are covered by Rule S1118.

        Attachments

        1.
        VB.NET RSPEC-4901 Language-Specification Active Unassigned

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: