When I have class containing a static constructor, is that constructor called when the assembly containing the class is first loaded or when the first reference to that class is hit?
When the class is accessed for the first time.
Static Constructors (C# Programming Guide)
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.
It's not quite as simple as you might expect despite straightforward documentation. Jon Skeet's article http://csharpindepth.com/Articles/General/Beforefieldinit.aspx goes into this question in details.
Summary:
Static constructor is guaranteed to be executed immediately before the first reference to a member of that class - either creation of instance or own static method/property of class.
Note that static initilaizers (if there is no static constructor) guaranteed to be executed any time before first reference to particular field.
The static constructor is called before you use anything in the class, but exactly when that happens is up to the implementation.
It's guaranteed to be called before the first static member is accessed and before the first instance is created. If the class is never used, the static constructor is not guaranteed to be called at all.
In case static method is called from parent class, static constructor will not be called, althogh it is explicitly specified. Here is an example b constructor is not called if b.methoda() is called.
static void Main(string[] args)
{
b.methoda();
}
class a
{
public static void methoda()
{
//using initialized method data
}
}
class b : a
{
static b()
{
//some initialization
}
}
There seems to be a gotcha with static constructors that is answered elsewhere but took a while to digest into a simple explanation. All the docs and explanations claim the static constructor/intializers are "guaranteed" to run before the first class is instantiated or the first static field is referenced. The gotcha comes in when you try to put a static singleton in the class that creates an instance of itself (chicken/egg). In this case the static constructor ends up being called after the instance constructor - and in my case the instance constructor contained code that relied on some static data.
Static constructor called after instance constructor?
Static constructor can run after the non-static constructor. Is this a compiler bug?
(the answer for me was to put the singleton in a separate class or manually initialize the static data in the instance constructor before it is required)
Related
I'm encountering this error:
'LnkScript.LnkScript.KillstreakHud.KillstreakHud(InfinityScript.Entity)': a static constructor must be parameterless C:\Users\home\Desktop\LnkScripts.cs 61 20 LnkScript
My source code:
public class KillstreakHud : BaseScript
{
static KillstreakHud(Entity player)
{
string killstreak = "^3Killstreak:^3" + player.GetField<int>("killstreak").ToString();
HudElem hudelem = HudElem.CreateFontString(player, "hudsmall", 1f);
HudElem.SetPoint("TOPCENTER", "TOPCENTER");
HudElem.SetText(killstreak);
base.OnInterval(300, delegate
{
killstreak = "^3Killstreak:^3" + player.GetField<int>("killstreak").ToString();
hudelem.SetText(Killstreak);
return true;
});
}
}
Clearly, my static constructor is not parameterless, and the compiler takes umbrage at this fact. But why?
A static constructor must be parameterless because nothing ever calls it, it is invoked when you access a static member or create an instance of the class, but not directly (it is called by the runtime).
The solution: Remove your parameters, or make it non-static
Reference for static constructors: http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx
To make it non-static (note that it will need to be invoked directly with the new keyword now):
public KillstreakHud(Entity player)
{
...
}
In my opinion this is a limitation of the language. Static is something that is called once and does not change. There is no reason (apart from not implemented) why it cannot accept a parameter with the understanding that it will only ever use the parameter value once.
This limitation of the language is what has made such a mess in "public static class ConfigurationManager"
There are lost of people asking can I use a different config file?
If the constructor could accept a parameter then this would be easy
(but it can't so you have to make your own config)
The main purpose of declaring a data member static is that it should be available in all instances of the class. When a data member is shared among different instances it is imperative that data should be consistent among all the instances of the class.
And also there is no way to call static constructor explicitly.
Therefore the purpose of having a parameterized static constructor is useless.
a static constructor is implicitly called when:-
1. static data member is referenced.
or
2.object of the class containing static constructor is created.
visit https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
When I have a static field in my class:
public static int Counter = 0;
With a static constructor:
static Class() {
Counter++;
}
When I create an object of this class and check Class.Counter it shows me 1 which is correct.
But when I create another object of the same class, Class.Counter remains 1.
Why is that?
Because the static constructor is executed only once.
From C# Specification:
The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
An instance of the class is created.
Any of the static members of the class are referenced.
That is because you are incrementing your counter in static constructor, and it will be executed just once.
static constructor C# - MSDN
A static constructor is used to initialize any static data, or to
perform a particular action that needs to be performed once only.
It is called automatically before the first instance is created or any
static members are referenced.
You can fix it by incrementing in instance constructor like:
class Class
{
public static int counter = 0;
public Class()
{
counter++;
}
}
For thread-safety use Interlocked.Increment(ref counter); instead of counter++
Selman22 has it correct, here is a little more detail:
From MSDN
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only.
It is called automatically before the first instance is >created or any static members are referenced.
Static constructors have the following properties:
A static constructor does not take access modifiers or have parameters.
A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
A static constructor cannot be called directly.
The user has no control on when the static constructor is executed in
the program.
A typical use of static constructors is when the class is using a log
file and the constructor is used to write entries to this file.
Static constructors are also useful when creating wrapper classes for
unmanaged code, when the constructor can call the LoadLibrary method.
If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Reference url: http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx
I searched for this a lot, but none of the answers are clear (at-least for me!). Now I'm putting this question in SO, as I believe I can't get a more clarified answer anywhere else.
When should I use a private/static constructor in my class?
I'm fed up of usual answers, so please help me with some real-time examples and advantages/disadvantages of using these constructors.
Static constructors: used for initialising static members.
Private constructors: used when you only want a class to be instantiated from within its own code (typically in a static method). For example:
public class Thing
{
static int Number;
static Thing()
{
Number = 42; // This will only be called once, no matter how many instances of the class are created
}
// This method is the only means for external code to get a new Thing
public static Thing GetNewThing()
{
return new Thing();
}
// This constructor can only be called from within the class.
private Thing()
{
}
}
When should I use a private constructor in my class?
When you want a constructor, but don't want to expose it to the world. This could be because you have a factory method that calls the constructor (after validation), or because that constructor is called by ctor-chaining (i.e. public Foo(string) : this() { ...}).
Additionally, note that reflection code is often able to use a private constructor - for example serialization or ORM libraries.
Also, in early C# compilers, when you are writing what would now be a static class - having a private constructor was the only way of making it appear uncreatable.
When should I use a static constructor in my class?
When you need to initialize some static state prior to that state being accessed by instances or static methods.
Static constructor is used to intialize the static members of the class and is called when the first instance of the class is created or a static member is accessed for the first time.
Private constructor is used if you have overloads of the constructor, and some of them should only be used by the other constructors
If I have a static variable in a class:
public class MyClass {
private static MyObject = new MyObject();
public void MyMethod() {
// do some stuff
}
}
Can the variable be instantiated when it is declared, as in the above?
Your code is legal and works.
One thing to be aware of is that static constructors and initalizers don't run when your module is loaded, but only when needed.
MyObject will only be instantiated when you either create an instance of MyClass or access a static field of it.
10.5.5.1 Static field initialization
The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.
The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
· An instance of the class type is created.
· Any of the static members of the class type are referenced.
So as I understand it:
If there is no static constructor the calling of a static method may trigger the initializers but isn't required to do so if the static method uses no static field.
If there is a static constructor it must run when a static member is referenced, so calling of a static method triggers first the static field initializers and then the the static constructor.
Yes. Two particular things to note:
The static variables will be initialized in the order they appear in the class.
They are guaranteed to be initialized before any static constructor is called.
Section 10.5.5.1 of the C# spec goes into more detail in you are interested.
If you are asking if this is legal C#, then yes it is. And it will do what you think it will.
I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors?
static and public are orthogonal concepts (i.e. they don’t have anything to do with each other).
public simply means that users of the class can call that constructor (as opposed to, say, private).
static means that the method (in this case the constructor) belongs not to an instance of a class but to the “class itself”. In particular, a static constructor is called once, automatically, when the class is used for the first time.
Furthermore, a static constructor cannot be made public or private since it cannot be called manually; it’s only called by the .NET runtime itself – so marking it as public wouldn’t be meaningful.
Static constructor runs just once, before your class is instantiated. It's used if you want something to happen just once. A nice example would be a Bus class (similar to something they explain in MSDN article):
public class Bus
{
public static int busNo = 0;
static Bus()
{
Console.WriteLine("Woey, it's a new day! Drivers are starting to work.");
}
public Bus()
{
busNo++;
Console.WriteLine("Bus #{0} goes from the depot.", busNo);
}
}
class Program
{
static void Main(string[] args)
{
Bus busOne = new Bus();
Bus busTwo = new Bus();
}
// Output:
// Woey, it's a new day! Drivers are starting to work.
// Bus #1 goes from the depot.
// Bus #2 goes from the depot.
}
Static Constructor... It is guaranteed to be called "once" througout the life of the application/app Domain. It can contain statements that you want to be executed only once.
Public Constructor... Since we can not add access modifiers to a static constructor, a public constructor means you are talking about an instance constructor. If an instance constructor is public then the outside world can create its instances.
Other options are Internal ( can be called from within the library), Private ( from within the class only).
Static constructor called only the first instance of the class created but the public constructor called every time that instance of the class is created.
Static Constructor
A constructor declared using a static modifier is a static constructor.
A static constructor is used to initialize static data or to perform a
particular action that needs to be performed only once in the life cycle of
class. A static constructor is the first block of code to execute in class.
Static constructor executes one and only one time in the life cycle of
class. It is called automatically. The static constructor does not take
any parameters. It has no access to specifiers. It is not called
directly.
Instance or Public Constructor
Instance constructor is used to initialize instance data. Instance
constructor is called every time when the object of the class is created. It
is called explicitly. Instance constructor takes parameters. It has
access specifiers.
My source: Static Constructor Vs Instance Constructor in C#