Base Class Doesn't Contain Parameterless Constructor?

前端 未结 6 1903
别那么骄傲
别那么骄傲 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条回答
  •  -上瘾入骨i
    2021-02-01 01:38

    If your base class doesn't have a parameterless constructor, you need to call one from your derived class using base keyword:

    class A
    {
        public A(Foo bar)
        {
        }
    }
    
    class A2 : A
    {
        public A2()
            : base(new Foo())
        {
        }
    }
    

提交回复
热议问题