Calling the base constructor in C#

前端 未结 12 2601
南笙
南笙 2020-11-22 03:04

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that?

For exa

12条回答
  •  再見小時候
    2020-11-22 03:12

    You can also do a conditional check with parameters in the constructor, which allows some flexibility.

    public MyClass(object myObject=null): base(myObject ?? new myOtherObject())
    {
    }
    

    or

    public MyClass(object myObject=null): base(myObject==null ? new myOtherObject(): myObject)
    {
    }
    

提交回复
热议问题