静态构造函数 构造函数

namespace staticpublic
{
    /* 静态构造函数不能显示调用不带访问修饰符不带参数系统自动调用*/

    class Test
    {
        public static string strstatic;
        public string strinstence;
        public Test(string str)//2调用静态构造函数 ,4运行构造函数
        {
            strinstence = str;
        }
        static Test()//3先运行静态构造函数
        {
            strstatic = "静态方法";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Test t = new Test("我是实例成员 ");//1调用构造函数 5 运行构造函数创建对象
            Console.WriteLine(t.strinstence);
            Console.WriteLine(Test.strstatic);
            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/ggg34674/p/2581611.html