c# 静态构造函数与构造函数的调用先后

先上代码:

测试类:

        /// <summary>
        /// 构造函数
        /// </summary>
        public RedisHelper()
        {
            Console.WriteLine("构造方法");
        }

        static RedisHelper()
        {
            Console.WriteLine("静态构造方法");
        }

        public static void Test()
        {
            Console.WriteLine("Test方法");
        }

        public static void Test2()
        {
            Console.WriteLine("Test2方法");
        }    

调用方法:

            RedisHelper r = new RedisHelper();
            RedisHelper.Test();
            RedisHelper.Test2();

通过例子实践证明得到:

1.静态构造函数先于构造函数执行。

2.静态构造函数只执行一次。

 

原文地址:https://www.cnblogs.com/dawenyang/p/7145315.html