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.
Related
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.
I have the following basic object factory for when I want some members of a class hierarchy to have special construction code and any other members to have generic constructors.
My problem here is that TileFactory doesn't have the method GetInstance--my program won't compile if I try to call TileFactory.GetInstance(). Any advice?
public static class ObjectFactory<K>
{
public static T GetInstance<T>() where T : K
{
T obj = (T)Activator.CreateInstance(typeof(T));
return obj;
}
//snip
}
}
//snip
public static class TileFactory : ObjectFactory<Tile>
{
}
Why can't I inherit static classes?
Citation from here:
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)
Other opinions from channel9
Inheritance in .NET works only on instance base. Static methods are defined on the type level not on the instance level. That is why overriding doesn't work with static methods/properties/events...
Static methods are only held once in memory. There is no virtual table etc. that is created for them.
If you invoke an instance method in .NET, you always give it the current instance. This is hidden by the .NET runtime, but it happens. Each instance method has as first argument a pointer (reference) to the object that the method is run on. This doesn't happen with static methods (as they are defined on type level). How should the compiler decide to select the method to invoke?
(littleguru)
And as a valuable idea, littleguru has a partial "workaround" for this issue: the Singleton pattern.
http://www.dofactory.com/Patterns/PatternSingleton.aspx
There is no inheritance for static things. A workaround is to use singletons.
By default constructors will be called whenever class is called, how make it to be called only once without making use of static constructors?
A static constructor is only called once per class. An instance constructor is called every time an instance of the class is created.
To me, it's unclear why you can't use the static constructor.
Use the singleton pattern.
Take a look at
http://www.yoda.arachsys.com/csharp/singleton.html
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
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?