zoukankan      html  css  js  c++  java
  • C#静态构造函数调用机制

    https://blog.csdn.net/cjolj/article/details/56329230

    若一个类中有静态构造函数,在首次实例化该类或任何的静态成员被引用时,.NET自动调用静态构造函数来初始化该类。注意是“首次”,即继续实例化该类时,不会调用该类的静态构造函数。

    1、实例化时调用静态构造函数
    /// <summary>
    /// 静态构造函数
    /// <remarks>
    /// Main主函数在类StaticConstructorTest之中:
    ///
    /// 1.程序被加载时,首先初始化了【静态字段】和【静态构造器】
    /// 【1】调用静态字段_count;
    /// 【2】调用静态构造函数 static StaticConstructorTest();
    /// 【3】调用Main下的obj1代码;
    /// </remarks>
    /// </summary>
    class StaticConstructorTest
    {
    private static int _count = 0;

    static StaticConstructorTest()
    {
    _count++;
    Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
    }

    StaticConstructorTest()
    {
    _count++;
    Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
    }

    static void Main(string[] args)
    {
    StaticConstructorTest obj1 = new StaticConstructorTest();
    StaticConstructorTest obj2 = new StaticConstructorTest();
    Console.WriteLine(_count);
    Console.ReadKey();
    }
    }


    2、引用静态成员时调用构造函数
    /// <summary>
    ///
    /// 静态构造函数:引用静态成员时调用构造函数
    ///
    /// <remarks>
    /// Main主函数在类StaticConstructorTest之中:
    ///
    /// 1.在进入main主函数之前,首先初始化了【静态字段】和【静态构造器】
    /// 【1】调用静态字段_count;
    /// 【2】调用静态构造函数 static StaticConstructorTest();
    ///
    /// </remarks>
    /// </summary>
    class StaticConstructorTest
    {
    private static int _count = 0;

    static StaticConstructorTest()
    {
    _count++;
    Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
    }

    StaticConstructorTest()
    {
    _count++;
    Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
    }

    static void Main(string[] args)
    {
    Console.WriteLine(_count);
    Console.ReadKey();
    }
    }

    3.实例化时调用静态构造函数(将主函数移到类外面)

    /// <summary>
    /// 静态构造函数
    /// <remarks>
    /// 注意:Main主函数在类StaticConstructorTest之外:
    ///
    /// 1. 在 obj1 对象被创建前,StaticConstructorTest 加载静态字段:
    /// 【1】调用静态字段_count;
    /// 【2】调用静态构造函数 static StaticConstructorTest();
    /// 【3】调用构造函数 构建obj1;
    ///
    /// 2.如果注释掉 obj1 和 obj2的构造代码(即:直接访问类的静态属性时,触发初始化【静态字段】和【静态构造器】)
    /// 即:直接调用 访问类静态属性Count的代码 Console.WriteLine(StaticConstructorTest.Count),
    /// 则调用顺序:
    /// 【1】调用静态字段_count;
    /// 【2】调用静态构造函数 static StaticConstructorTest();
    /// </remarks>
    /// </summary>
    public class StaticConstructorTest
    {
    private static int _count = 0;

    public static int Count
    {
    get { return StaticConstructorTest._count; }
    }
    static StaticConstructorTest() // static ctor
    {
    _count++;
    Console.WriteLine(string.Format("static constructor.Count is {0}", _count));
    }
    public StaticConstructorTest() // public ctor
    {
    _count++;
    Console.WriteLine(string.Format("normal constructor.Count is {0}", _count));
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    StaticConstructorTest obj1 = new StaticConstructorTest();
    StaticConstructorTest obj2 = new StaticConstructorTest();
    Console.WriteLine(StaticConstructorTest.Count);
    Console.ReadKey();
    }
    }


    4.引用静态成员时调用构造函数(将主函数移到类外面)

    注释掉obj1和obj2的实例化代码

    class Program
    {
    static void Main(string[] args)
    {
    //StaticConstructorTest obj1 = new StaticConstructorTest();
    //StaticConstructorTest obj2 = new StaticConstructorTest();
    Console.WriteLine(StaticConstructorTest.Count);
    Console.ReadKey();
    }
    }

  • 相关阅读:
    网络游戏
    嘎嘎
    Failed to install *.apk on device 'emulator-5554': timeout
    安卓开发真机遇到Failed to install Spaceassault.apk on device 'HT1CKV205198': timeout 测试机没有问题
    java匿名内部类
    TextView tv01=(TextView)this.findViewById(R.id.TextView01); tv01.setText("设置文字背景色");
    android 项目中出现红色感叹号的解决方法
    使用block来解决实现switch解决字符串
    oc中的block
    不可变数组或者可变数组进行排序
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/10024493.html
Copyright © 2011-2022 走看看