Skip to content
Amal Hashim
All posts
Snippet

C# Inheritance

The syntax of class inheritance in C# and the main rules to remember.

1 min read#.NET#C#

Inheritance lets one class reuse and extend another. In C# you use a colon.

csharp
class A
{
    int aVariable;
    public A()
    {
    }
    public void SomeMethodA()
    {
    }
}

class B : A
{
    int bVariable;
    public B()
    {
    }
    public void SomeMethodB()
    {
    }
}

B inherits from A. In other words, B extends A. B gets aVariable and SomeMethodA, and adds bVariable and SomeMethodB.

Rules

  • C# supports single inheritance. A class can inherit from only one class, but it can implement multiple interfaces.
  • Constructors are not inherited.
  • Inherited methods can be overridden.
  • A class can inherit only from another class, not from a struct.
  • A class with no explicit base class inherits from Object.