C# static class constructor
Asked Answered
F

8

166

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.

Feverroot answered 17/7, 2011 at 4:2 Comment(0)
B
284

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

.

Brindabrindell answered 17/7, 2011 at 4:6 Comment(2)
Note that there's no requirement that YourClass be static.Locale
To be fair, the original poster did specifically ask about a constructor for a static class.Brindabrindell
B
48

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.

Brigadier answered 17/7, 2011 at 4:5 Comment(1)
Actually "It is called automatically BEFORE the first instance is created or any static members are referenced" msdn.microsoft.com/en-us/library/k9x6w0hc.aspxBearden
F
5

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();
Flintlock answered 7/8, 2015 at 9:57 Comment(0)
G
2

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

like this:

static class YourClass
{
    static YourClass()
    {
        //initialization
    }
}
Guiana answered 14/11, 2020 at 13:32 Comment(0)
M
1

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

Misbegotten answered 9/4, 2019 at 6:40 Comment(0)
G
0

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
    }

}
Gametocyte answered 12/6, 2016 at 10:23 Comment(1)
static constructor doesn't have the access modifier, this will be a compilation errorNicotine
T
0

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

Tamis answered 16/10, 2022 at 21:33 Comment(0)
C
0

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
}
Comanchean answered 10/3, 2023 at 15:42 Comment(1)
valid for NET 6 ?Traipse

© 2022 - 2024 — McMap. All rights reserved.