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
{
public SubClass(string BaseString, string SubString) : base(BaseString)
This constructor in the derived class says that when you receive two arguments BaseString
and SubString
, call the base class's constructor with BaseString
.
Thus, doing
public SubClass(string a, string b) : base(BaseString)
doesn't work, because you are telling it to call the base class's constructor with BaseString
but there is no argument called BaseString
.
If you want to instantiate the derived class without passing a String to the base class constructor, the base class needs a default constructor:
public BaseClass() { }