简体   繁体   中英

Static Class and Private Constructor

I am not able to understand this. I tried doing a small example in VS2010 as below.

"Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor"

Does this statement means a class with private constructor and one or more static methods like below is static class ? I know we call a class static only when static keyword is mentioned in class.

Also, we cannot inherit the below class and also we can't instantiate this class right ?

public class Base
{
    private Base() { Console.WriteLine(" I am from normal Base constructor"); }
    static void NewMethod() { Console.WriteLine("Hey I am from Static Base"); }
    public void New() { } 
}

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor

This statement is attempting to get across the right idea but failing to do so. A static class is not like a class with only static members and a private constructor. Here's a class with static members and private constructor:

class X
{
    private X() {}
    public static X Y() { return new X(); }
}

But that's not at all like a static class! The author of that statement seems to think that having a private constructor prevents you from making instances, but of course it does not.

A more accurate statement would be:

Creating a static class is much like creating a class that is both abstract and sealed, and contains no instance members.

And in fact, when the C# compiler generates the code for a static class, that's precisely what it does: it marks the class as both abstract (so it cannot be instantiated directly) and sealed (so that it cannot be extended).

I note that it is not legal for you to declare a class both abstract and sealed yourself; the only way to do so in C# is to make a static class.

I will bring the misleading sentence to the attention of the MSDN documentation managers. Thanks for pointing it out.

What it means is that:

public static class Foo
{
    public static void Bar() { }
}

is essentially the same as

public class Foo
{
    private Foo() { }
    public static void Bar() { }
}

because if the class only has a private constructor, it cannot be instantiated outside the class.

"Does this statement mean a class with private constructor and one or more static methods like below is a static class?"

The answer is No, and one difference is explained in exactly the next sentence after the one you are citing from MSDN :

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added.


It means that you will get a compiler error in Class2 shown below.

public class Class1
{
    private Class1() { }
    public static void Method() { }
    private string member; // valid, but pointless
}

public static class Class2
{
    public static void Method() { }
    private string member; // error CS0708
}

More important, although Class1 has a private constructor, it may still be instantiated:

public class Class1
{
    private Class1() { }

    private static Class1 instance = new Class1();

    public static Class1 Instance
    {
        get { return instance; }
    }
}

A static class on the other hand, may never be instantiated.

You can not inherit as there is no public constructor, only a private constructor exists. For the same reason you can not create an instance.

Within the scope of this question, they can be seen as the same. Can you call it a static class; I think officially you do not since it is not marked as static in the class definition. But in the perspective of functionality you may call it static .

With your example, the only way the New() method can be called is if you add another method to return a Base instance since Base can't be instantiated by another class. Not having that extra method makes it functionally the same as a static class.

Where did you get the statement:

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor

What it is trying to say is that you can not create an instance of a static class.

The way to create a static class is to use the static keyword. All members in the class must also be static.

public static class MyStaticClass
{
      static MyStaticClass() { /* Constructor.  Optional. */ }
      public static void MyMethod() { ... }
      public static int MyProperty{ get; set; }
}

Note again, you can not create an instance of this class. The following call will not compile:

new MyStaticClass();

A static class can never be instantiated, participate in inheritance, or use interfaces. But a class, even with a private constructor can still inherit, where a static class cannot. And a class with a private constructor can still instantiate an instance of itself. A static cannot do that. So NO they are not the same.

I do not understand why Microsoft at times still doesn't understand what it created, because they are the ones that wrote that description (as Eric Lippert mentioned)?

The other confusion is whether a Static Constructor is Private or Public? That would help explain behavior because like the default public constructor in every class, there is also a default constructor for every static class. To me that implies the static constructor would be public, but it is not. It cannot be accessed or called, and can be overwritten.

Because the static class is translated when compiled into an abstract class that's sealed, that suggests its static constructor really is just another default public class constructor without parameters that can be called only once. I could be wrong. But if that's the case, the whole private constructor explanation makes no sense.

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