C#小知识之有趣的类型静态构造器

这篇文章主要介绍了C#小知识之有趣的类型静态构造器,本文直接给分实例代码,然后分析了C#中的这一个有趣的现象,需要的朋友可以参考下

这是C#中一个有趣的现象,也许您从中可以窥见些许CLR在构造类型时的行为,以及JIT编译的触发式编译过程。

看下面一段代码:

复制代码 代码如下:

class Program
    {
        static void Main()
        {
            myValueType1 type1 = new myValueType1();
            Console.WriteLine(myValueType1.myInt);
            Console.WriteLine("**********************");
            myValueType2 type2 = new myValueType2();
            type2.myInt =23;
            Console.WriteLine(type2.myInt);
            Console.WriteLine("**********************");
            myValueType3 type3 = new myValueType3();
        }
    }

    struct myValueType1
    {
        static myValueType1()
        {
            Console.WriteLine("Hello from myValueType1");
           // myInt = 111;
        }
        public static Int32 myInt;
    }

    struct myValueType2
    {
        static myValueType2()
        {
            Console.WriteLine("Hello from myValueType2");
        }
        public Int32 myInt;
    }

    struct myValueType3
    {
        static myValueType3()
        {
            Console.WriteLine("Hello from myValueType3");
            myInt = 333;
        }
        public static Int32 myInt;
    }

以上就是C#小知识之有趣的类型静态构造器的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程