jM2.me
jM2.me

Reputation: 3969

C# static class constructor

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.

Upvotes: 171

Views: 178415

Answers (8)

yakya
yakya

Reputation: 5220

In C# 9, there is ModuleInitializer attribute which is much better than static constructors. The advantages are listed below as quoted from the linked site:

  • Enable libraries to do eager, one-time initialization when loaded, with minimal overhead and without the user needing to explicitly call anything
  • One particular pain point of current static constructor approaches is that the runtime must do additional checks on usage of a type with a static constructor, in order to decide whether the static constructor needs to be run or not. This adds measurable overhead.
  • Enable source generators to run some global initialization logic without the user needing to explicitly call anything

These are super-important points and one can use the attribute like the following in their static class:

[ModuleInitializer]
public static void Init()
{
    // Logic
}

Upvotes: 0

myteardrop4u
myteardrop4u

Reputation: 53

Static classes cannot have instance constructors (unlike the accepted answer). However, a class can have a static constructor. That is totally different.

Upvotes: 0

Hasan Fathi
Hasan Fathi

Reputation: 6106

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

like this:

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

Upvotes: 2

Pavan Tiwari
Pavan Tiwari

Reputation: 3187

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
    }

}

Upvotes: 0

Shivang Gupta
Shivang Gupta

Reputation: 77

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

Upvotes: 1

Jared S
Jared S

Reputation: 3416

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

.

Upvotes: 289

Kumod Singh
Kumod Singh

Reputation: 2163

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();

Upvotes: 5

Sven
Sven

Reputation: 22703

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.

Upvotes: 50

Related Questions