C# static class constructor

C#ClassStaticConstructor

C# Problem Overview


Is there a work around on how to create a constructor for static class?

I need some data to be loaded when the class is initialized but I need one and only one object.

C# Solutions


Solution 1 - C#

C# has a static constructor for this purpose.

static class YourClass
{
    static YourClass()
    {
        // perform initialization here
    }
}

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

MSDN link

.

Solution 2 - C#

A static constructor looks like this

static class Foo
{
    static Foo()
    {
         // Static initialization code here
    }
}

It is executed only once when the type is first used. All classes can have static constructors, not just static classes.

Solution 3 - C#

Yes, a static class can have static constructor, and the use of this constructor is initialization of static member.

static class Employee1
{
    static int EmpNo;
    static Employee1()
    {
        EmpNo = 10;
        // perform initialization here
    }
    public static void Add()
    { 
    
    }
    public static void Add1()
    { 
    
    }
}

and static constructor get called only once when you have access any type member of static class with class name Class1

Suppose you are accessing the first EmployeeName field then constructor get called this time, after that it will not get called, even if you will access same type member.

 Employee1.EmployeeName = "kumod";
        Employee1.Add();
        Employee1.Add();

Solution 4 - C#

Static constructor called only the first instance of the class created.

like this:

static class YourClass
{
    static YourClass()
    {
        //initialization
    }
}

Solution 5 - C#

You can use static constructor to initialization static variable. Static constructor will be entry point for your class

public class MyClass
{

    static MyClass()
    {

        //write your initialization code here
    }

}

Solution 6 - C#

We can create static constructor

static class StaticParent 
{
  StaticParent() 
  {
    //write your initialization code here

  }

}

and it is always parameter less.

static class StaticParent
{
    static int i =5;
    static StaticParent(int i)  //Gives error
    {
      //write your initialization code here
    }
}

and it doesn't have the access modifier

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjM2.meView Question on Stackoverflow
Solution 1 - C#Jared SView Answer on Stackoverflow
Solution 2 - C#SvenView Answer on Stackoverflow
Solution 3 - C#Kumod SinghView Answer on Stackoverflow
Solution 4 - C#Hasan FathiView Answer on Stackoverflow
Solution 5 - C#Pavan TiwariView Answer on Stackoverflow
Solution 6 - C#Shivang GuptaView Answer on Stackoverflow