Static constructors in C# are constructors implemented to be invoked only once and only during the creation of the reference for a static member implemented in the class. The primary function for a static constructor is to initialize the static members for the class and only once execution. The static constructor as the name suggests does not allow explicit control to the user but is executed automatically upon the invoke of the instance of the class, Further, the static constructor doesn’t take any parameter or access declaration in its definition, thus it can’t be called directly. Static constructors cannot be inherited or overloaded and only accessible to the CLR (Common Language Runtime).
Following are some features and uses of static constructors in c# explained in details:
The following features describe the static constructor:
Static constructors find its major use in log programs where it is used to write the parameter entries initialized during each instance. Static constructors are the ideal positions to create database connections as they are loaded first and remain static throughout. In C# programming language the static constructors following the following Syntax.
Syntax:
class ABC { //defining the static constructor using the same name as class static ABC() { //constructor specific code comes here } }
Here are some basic working principle of static constructor in C# which are as follows:
Here are some of the examples of static constructor in C# which are given below:
Code:
using System; namespace HappyConstructor { class Happy { //let us declare and initialise the static data members private static int id = 7; public static int Id { get { return id; } } public static void printVariable() { Console.WriteLine("Happy.id = " + id); } static void Main(string[] args) { //let us print the value of ID from the class Happy. printVariable (); } } }
Output:
Explanation: In the above, the static member id is declared for use and initialized to the initial value of 10.
Code:
using System; namespace Happy1Constructor { class Happy1 { private static int Id; //data member (id) is set conditionally here to show actions of a Static constructor static Happy1 () { if (Happy1.Id < 10) { Id = 25; } else { Id = 1000; } Console.WriteLine("Static Constructor for Class Happy Called.."); } public static void print() { Console.WriteLine("Happy1.Id = " + Id); } public static void Main(string[] args) { // the value of Id is displayed as Happy1.print(); } } }
Output:
Explanation: In the example above the constructor is conditionally dependent upon the Happy.cs file generated in example1. Here the static constructor initializes itself. since the value is in the first case the Id field generated is 7 and as per the conditional operator if the value of the field is less than 10 then the value for the Id field for the Happy1 constructor shall be 25. This is a classic example of initializing a static constructor upon the first instance of loading of the class. The static constructor makes use of this feature to preload the input parameters for the program referencing.
A static constructor is used to initialize any static data and or in performance of any particular actions that need to be performed once and only once for the program. This constructor is called upon before any of the objects of the class is initiated or any of the members are loaded on to the run time environment.
The above is the detailed content of Static Constructor in C#. For more information, please follow other related articles on the PHP Chinese website!