Base Class Doesn't Contain Parameterless Constructor?

前端 未结 6 1965
别那么骄傲
别那么骄傲 2021-02-01 01:11

I\'m making my constructors a bit more strict by removing some of my empty constructors. I\'m pretty new to inheritance, and was perplexed with the error that I got: Base Class

6条回答
  •  耶瑟儿~
    2021-02-01 01:25

    It has to call some constructor. The default is a call to base().

    You can also use static methods, literals, and any parameters to the current constructor in calls to base().

      public static class MyStaticClass
        {
            public static int DoIntWork(string i)
            {
                //for example only
                return 0;
            }
        }
    
        public class A
        {
            public A(int i)
            {
            }
        }
    
        public class B : A
        {
            public B(string x) : base(MyStaticClass.DoIntWork(x))
            {
            }
        }
    

提交回复
热议问题