简体   繁体   English

C# static class 构造函数

[英]C# static class constructor

Is there a work around on how to create a constructor for static class?是否有关于如何为 static class 创建构造函数的解决方法?

I need some data to be loaded when the class is initialized but I need one and only one object.我需要在 class 初始化时加载一些数据,但我需要一个且只有一个 object。

C# has a static constructor for this purpose.为此,C# 有一个 static 构造函数。

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

From MSDN :来自 MSDN

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. static 构造函数用于初始化任何 static 数据,或执行只需要执行一次的特定操作。 It is called automatically before the first instance is created or any static members are referenced在创建第一个实例或引用任何 static 成员之前自动调用它

MSDN link MSDN 链接

. .

A static constructor looks like this static 构造函数看起来像这样

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.所有类都可以有 static 构造函数,而不仅仅是 static 类。

Yes, a static class can have static constructor, and the use of this constructor is initialization of static member.是的,一个 static class 可以有 static 构造函数,这个构造函数的使用是初始化 ZA8125ZDF1D458E95DC2974FE

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当您可以访问 static class 和 ZA2F2ED4F8EBC2CBBDZC2A1 类的任何类型成员时,只调用一次static构造函数

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.假设您正在访问第一个 EmployeeName 字段,然后这次调用构造函数,之后它将不会被调用,即使您将访问相同类型的成员。

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

Static Constructor Static 构造函数

A constructor declared using static modifier is a static constructor.使用 static 修饰符声明的构造函数是 static 构造函数。 A static constructor is use to initialize static data or to perform a particular action that need to be performed only once in life cycle of class. static 构造函数用于初始化 static 数据或执行在 class 的生命周期中只需要执行一次的特定操作。 Static constructor is first block of code to execute in class. Static 构造函数是在 class 中执行的第一个代码块。 Static constructor executes one and only one time in life cycle of class. Static 构造函数在 class 的生命周期中只执行一次。 It is called automatically.它是自动调用的。 Static constructor does not take any parameters. Static 构造函数不带任何参数。 It has no access specifiers.它没有访问说明符。 It is not called directly.它不是直接调用的。

C# has a static constructor for this purpose .为此,C# 有一个static 构造函数

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

We can create static constructor我们可以创建 static 构造函数

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并且它没有访问修饰符

Static constructor called only the first instance of the class created. Static constructor仅调用创建的 class 的第一个实例。

like this:像这样:

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

You can use static constructor to initialization static variable.您可以使用 static 构造函数来初始化 static 变量。 Static constructor will be entry point for your class Static 构造函数将成为您的 class 的入口点

public class MyClass
{

    static MyClass()
    {

        //write your initialization code here
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM