A typical constructor is used to set the value of an object’s instance-level data at the time of creation.However, what would happen if you attempted to assign the value of a static point of data in a typical constructor?
You might be surprised to find that the value is reset each time you create a new object!
To illustrate, assume you have updated the SavingsAccount class constructor as follows (also note we are no longer assigning the currInterestRate field inline):
Now, assume you have authored the following code in Main():
If you execute the previous Main() method, you would see that that the currInterestRate variable is
reset each time you create a new SavingsAccount object, and it is always set to 0.04. Clearly, setting the
value of static data in a normal instance-level constructor sort of defeats the whole purpose. Every time
you make a new object, the class-level data is reset! One approach to setting a static field is to use
member initialization syntax, as you did originally:
This approach will ensure the static field is assigned only once, regardless of how many objects you
create. However, what if the value for your static data needed to be obtained at runtime? For example, in
a typical banking application, the value of an interest rate variable would be read from a database or
external file. To perform such tasks requires a method scope such as a constructor to execute the code
For this very reason, C# allows you to define a static constructor, which allows you to safely set the
values of your static data. Consider the following update to our class:
Simply put, a static constructor is a special constructor that is an ideal place to initialize the values
of static data when the value is not known at compile time (e.g., you need to read in the value from an
external file, a database, generate a random number, or whatnot). If you were to rerun the previous
Main() method, you would find the output you expect. Note that the message “In static ctor!” only prints
out one time, as the CLR calls all static constructors before first use (and never calls them again for that
instance of the application):
***** Fun with Static Data *****
In static ctor!
Interest Rate is: 0.04
Interest Rate is: 0.08
Here are a few points of interest regarding static constructors:
- A given class may define only a single static constructor. In other words, the static constructor cannot be overloaded.
- A static constructor does not take an access modifier and cannot take any parameters.
- A static constructor executes exactly one time, regardless of how many objects of the type are created.
- The runtime invokes the static constructor when it creates an instance of the class or before accessing the first static member invoked by the caller.
- The static constructor executes before any instance-level constructors.
Given this modification, when you create new SavingsAccount objects, the value of the static data is preserved, as the static member is set only one time within the static constructor, regardless of the number of objects created.