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.
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# 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
.
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.
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();
Static constructor
called only the first instance of the class created.
like this:
static class YourClass
{
static YourClass()
{
//initialization
}
}
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
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
}
}
Static classes cannot have instance constructors (unlike the accepted answer). However, a class can have a static constructor. That is totally different.
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
}
© 2022 - 2024 — McMap. All rights reserved.
YourClass
bestatic
. – Locale