Constructor Parameters and Inheritance

后端 未结 4 1017
生来不讨喜
生来不讨喜 2020-12-09 08:35

New to OOP and I\'m confused by how derived-class constructors work when inheriting from a base class in C#.

First the base class:

class BaseClass
{
         


        
4条回答
  •  Happy的楠姐
    2020-12-09 09:40

    If you want to initialize BaseString with default value, then pass that value to base class constructor (thus you don't have to pass that parameter to derived class constructor)

    public SubClass(string SubString) : base(null)
    

    Or define parameterless constructor in base class.

    Also about parameter naming - it does not matter what name has parameter, which you are passing to derived constructor. The only thing which matter is a value you are passing to base constructor.

    public SubClass(string AnotherString, string SubString) : base(AnotherString)
    

提交回复
热议问题