Can we have a private constructor in a static class? - c#

I have a doubt that a static class can contain a private constructor.

Static classes cannot have instance constructors
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
The following list provides the main features of a static class:
Contains only static members.
Cannot be instantiated.
Is sealed.
Cannot contain Instance Constructors.

A static class cannot have any instance constructor ( see CS0710 ), whether it be public, private, protected or internal.
See the following article for more info.
Static Classes and Static Class Members (C# Programming Guide)

What would this constructor do? The class is static, so it is never instantiated. You can have a static constructor on a non-static class to initialize static fields, but on a static class, the only constructor that makes sense is the static constructor, and that gets called be the CLR.
Addition: Jon Skeet posted an article about the timing of the initialization of the static class (normally it's initialized on first use, but sometimes you want to initialize it when the program starts) and a possible change in .net 4.

Your doubt is correct.
A static class can only have a static constructor and public/private does not apply since your code can never call this constructor (the CLR does).
So you may not use a access modifier (public/private/...) on a static constructor.

rule is static classes cannot have instance constructors

Related

Do C# classes default to instance or static classes if not specified?

When declaring a class in C# without specifying if the class is a static or non-static class which will it default to?
EDIT: Here's an article I wrote based on this discussion.
https://hackernoon.com/c-static-vs-instance-classes-and-methods-50fe8987b231
If you don't declare the class as static then its members can be either static or non-static.
A static class can only have static members.
You can invoke non-static members only on instances of the class.
You can invoke static members only on the class itself.
Also, in your class declaration, there are no parentheses.
If the "static" is not specified, it will require an instance of the class to be used (Unless the member itself is specified as static).
If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable.
From: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes
A class is always an instance class unless you specify otherwise

Does Static class create an instance? msdn says i doesn't, but why constructor then?

I came across trying to understand the difference between singleton object and static class
and the simplest information I see everywhere is that static class DOES NOT create an instance while singleton requires to
But why the hell can I have the static constructor from static class? what does that mean? doesn't it create an instance?
if you run simple code with breakpoint on static class constructor you will see it reaches it
I'm confused, anybody?
But why that hell can I have the static constructor from static class? what does that mean? doesn't it create an instance?
No. The static constructor allows you to initialize static members of the class (basically, the static state for that class).
With the singleton pattern, the static constructor (or a static inline initializer) often creates an instance, but that instance is still created via the normal, non-static constructor. It's then stored within a static variable (the single "instance" variable).
Lazy-initialized singletons will avoid that, and initialize the static variable on demand.
A static class is a different thing - a static class will never work as a singleton, since you can't create an instance of a static class. Static classes are specifically intended to be used when you will never create an instance.
A singleton will (typically) be created by having a non-static class, but using a private constructor (so the instance can only be created within that class). There will be a static property used to retrieve a single instance of that class. The class instance will either be created on demand or within the static constructor.
The static constructor is just a place where you can initialise your static member variables. You don't need a static constructor - you can initialise the static member variables inline instead, but I think it's tidier to put them in the static constructor.
Remember that even though you don't instantiate the static class, you do instantiate its static members, and it's nice to have a place to do it. This is an improvement on the way you have to do it in C++.
Note that your class doesn't need to be static in order to have a static constructor. You can have a non-static class that provides both an ordinary constructor and a static constructor. Same rule applies.

C# how does Console work?

This is a pretty basic question,
But how does, for example
Console.Write("test");
work?
Console is a class, not an object.
I'm using console as an example because it is commonly used, but I've seen many examples of using Class.method() instead of object.method().
That method is called static method: Static Classes and Static Class Members (C# Programming Guide).
You don't need an instance to call static class member:
A non-static class can contain static methods, fields, properties, or
events. The static member is callable on a class even when no instance
of the class has been created. The static member is always accessed by
the class name, not the instance name. Only one copy of a static
member exists, regardless of how many instances of the class are
created. Static methods and properties cannot access non-static fields
and events in their containing type, and they cannot access an
instance variable of any object unless it is explicitly passed in a
method parameter.
Write is a static method, so you invoke it on the type instead of on an instance. See documentation.
Write is a static method in the Console class, not an instance method.
Console.Write is a static method which writes to the console without adding a new line.

Differences between singleton/static class/instance class with a private ctor?

The below seem to be very similar:
What differences are there between a static and private ctor in a class?
Furthermore, what's the difference between a singleton and an instance class with a static or private constructor?
Both static constructors (see Fourth version) and private constructors can be used to implement the Singleton design pattern.
A static constructor is called the first time the class type is mentioned in any way. Could be used for filling static dictionaries, for example. These constructors can not be called explicitly.
Private constructors on the other hand, can only be called from within the class itself. This can be used to restrict or monitor the creation of new instances of that class, by creating factory methods, for example.
As for the singleton question, Oren A about summed that up.

C# inheritence and static classes

Why can't a static class be inherited into a normal class?
If B inherits from (is a subclass of) A, that means an instance of B can be stored in a variable of type A, and its virtual methods will call those of class B.
For static classes, you don't have the concept of an instance of the class, so there's no way to inherit. You might have better luck with a static (singleton) reference to a regular class.
From Static Classes and Static Class Members (C# Programming Guide)
Creating a static class is therefore
basically the same as creating a class
that contains only static members and
a private constructor. A private
constructor prevents the class from
being instantiated. The advantage of
using a static class is that the
compiler can check to make sure that
no instance members are accidentally
added. The compiler will guarantee
that instances of this class cannot be
created.
Static classes are sealed and
therefore cannot be inherited. They
cannot inherit from any class except
Object. Static classes cannot contain
an instance constructor; however, they
can contain a static constructor.
As an alternative to inheriting from a static class, you can assign extension methods to interfaces.
You can not inherit a static class - The reason is simple. Static classes are marked as abstract and sealed in compiled IL which can be neither instantiated nor inherited.
This is actually by design. There seems to be no good reason to inherit a static class. It has public static members that you can always access via the class name itself. The only reasons I have seen for inheriting static stuff have been bad ones, such as saving a couple of characters of typing.
There may be reason to consider mechanisms to bring static members directly into scope (and we will in fact consider this after the Orcas product cycle), but static class inheritance is not the way to go: It is the wrong mechanism to use, and works only for static members that happen to reside in a static class.
(Mads Torgersen, C# Language PM)
Source:
Why can't I inherit static classes?

Categories