Static class& class init

Today, when i read some part of the system, i found a static class, so i record here

static class  or  non static class  can has static constructor.

static className{...}

When before create instance or use function member, Static Construct run and  only run once.

For some add info: as follow
Static members are initialized before the static member is accessed for the first time and before the static constructor, if

there is one, is called.

    TestStatic ts = new TestStatic();

    public class TestStatic
    {
        private static int teststaticint = 1; // first init

        private int testint = 2; //third init

        static TestStatic() // second init
        {
            teststaticint = 10;
        }

        public  TestStatic()  //fourth init
        {
            testint = 20;
            teststaticint = 30;
        }

        public static void  PrintfMe()
        {
            Console.WriteLine("in static method");
        }
    }

posted on 2011-11-17 17:02  子虚乌有  阅读(330)  评论(0)    收藏  举报