简体   繁体   中英

Static Class vs Protected Constructor

I Am getting a warning message in my class, like

在此处输入图片说明

Add a Protected constructor or the static` keyword to the class declaration

Solution

The error is gone, after I tried both below ways, .

static class without constructor

public static class Program    {
    }

Non static class with protected using constructor

public  class Program
    {
        protected Program() { }
    }

Question:

So What is the difference of Static Class vs Protected Constructor which is mentioned in my above solution? And which one is best to use?

A static class doesn't need an instance to access its members. A static class cannot have instance members (eg public int MyNumber; is not allowed on a static class because only static members are allowed on a static class). Both instance and static members are allowed on a non-static class though. A class with a protected constructor can only have an instance created by itself or something that inherits from it.

public class Program
{
    protected Program()
    {
        // Do something.
    }

    public static Program Create()
    {
        // 100% Allowed.
        return new Program();
    }

    public void DoSomething()
    {

    }
}

public static class AnotherClass
{
    public static Program CreateProgram()
    {
        // Not allowed since Program's constructor is protected.
        return new Program();
    }
}

public class SubProgram : Program
{
    protected SubProgram()
    {
        // Calls Program() then SubProgram().
    }

    public new static Program Create()
    {
        // return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
        return new SubProgram();
    }
}

Program.Create();               // Can be called since Create is public and static function.
Program.DoSomething()           // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething();             // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram();   // Can't be called since Program's constructor is protected.
SubProgram.Create();            // Can be called since SubProgram inherits from Program.

As for performance, this distinction doesn't really have much to do with performance.

You probably only have static members in the class and the code analyser assumes that your intention is to not be able to create instances of the class so it is asking you to either make the class static

public static class Program {
    //...static members
}

or put a protected/private constructor

public class Program {
    protected Program { //OR private

    }

    //...static members
}

to prevent instances of that class from being initialized.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.

Reference Static Classes and Static Class Members (C# Programming Guide)

The protected constructor means that only derived classes can call the constructor

and a private constructor wont allow any other classes to initialize the class with a private constructor

A static constructor is called when the class type is instantiated. The protected constructor is called when an instance of a class is created. The protected part means only classes that inherit the class can call it.

Static Constructor: Called once when the class type is instantiated and is used to initialize static members. Does not create an instance of the class.

Protected Constructor: A constructor that can be called only by the class or a class that inherits it.

The best practices for this is that you should have a static constructor for initializing static members and a protected constructor if you only want classes that inherit to be able to create an instance of your class. You can have both.

public class MyClass
{
    static readonly long _someStaticMember;
    private bool _param;
    static MyClass()
    {
        //Do Some Logic
        _someStaticMember = SomeValueCalculated;
    }
    protected MyClass(bool param)
    {
        _param = param;
    }
}
public class ChildClass: MyClass
{
    public ChildClass(bool param) : base(param);
}
public class NotChildClass
{
    public MyClass someObject = new MyClass(true); //Will Fail
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM