C#-类的成员初始化顺序

        public static async Task Main(string[] args)
        {
            new Child();

            Console.ReadKey();
            await Task.CompletedTask;
        }

        public static class Helper
        {
            public static string ValueWithLog(string s)
            {
                Console.WriteLine(s);
                return s;
            }
        }

        public class Base
        {
            private static string _baseStaticField = Helper.ValueWithLog($"{nameof(Base)}:{nameof(_baseStaticField)}");
            public static string BaseStaticProperty { get; set; } = Helper.ValueWithLog($"{nameof(Base)}:{nameof(BaseStaticProperty)}");
            
            private string _baseField = Helper.ValueWithLog($"{nameof(Base)}:{nameof(_baseField)}");
            public string BaseProperty { get; set; }= Helper.ValueWithLog($"{nameof(Base)}:{nameof(BaseProperty)}");
            static Base()
            {
                Helper.ValueWithLog($"{nameof(Base)}:static Base()");
            }

            public Base()
            {
                Helper.ValueWithLog($"{nameof(Base)}:public Base()");
            }
        }

        public class Child : Base
        {
            private static string _childStaticField = Helper.ValueWithLog($"{nameof(Child)}:{nameof(_childStaticField)}");
            public static string ChildStaticProperty { get; set; } = Helper.ValueWithLog($"{nameof(Child)}:{nameof(ChildStaticProperty)}");

            private string _childField = Helper.ValueWithLog($"{nameof(Child)}:{nameof(_childField)}");
            public string ChildProperty { get; set; } = Helper.ValueWithLog($"{nameof(Child)}:{nameof(ChildProperty)}");

            static Child()
            {
                Helper.ValueWithLog($"{nameof(Child)}:static Child()");
            }

            public Child()
            {
                Helper.ValueWithLog($"{nameof(Child)}:public Child()");
            }
        }

 

posted @ 2020-09-15 10:00  自笑非  阅读(163)  评论(0编辑  收藏  举报