Constructors and Inheritance - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Introduction

A subclass must declare its own constructors.

The base class's constructors are accessible to the derived class but are never automatically inherited.

For example, if we define Baseclass and Subclass as follows:

public class Baseclass
{
  public int X;

  public Baseclass () { }
  public Baseclass (int x) { this.X = x; }
}

public class Subclass : Baseclass { }

the following is illegal:

Subclass s = new Subclass (123);

Subclass must redefine any constructors it wants to expose.

It can call any of the base class's constructors with the base keyword:

public class Subclass : Baseclass
{
  public Subclass (int x) : base (x) { }
}

Related Tutorials