0
0
CsharpHow-ToBeginner · 4 min read

How to Use Inheritance in C#: Syntax and Examples

In C#, use class DerivedClass : BaseClass to create inheritance where DerivedClass gets properties and methods from BaseClass. This allows code reuse and easy extension of functionality.
📐

Syntax

Inheritance in C# is declared by using a colon : after the derived class name, followed by the base class name. The derived class inherits all accessible members from the base class.

  • BaseClass: The class to inherit from.
  • DerivedClass: The class that inherits features.
csharp
class BaseClass
{
    public void ShowMessage()
    {
        System.Console.WriteLine("Hello from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public void ShowDerived()
    {
        System.Console.WriteLine("Hello from DerivedClass");
    }
}
💻

Example

This example shows a base class Animal with a method Speak. The derived class Dog inherits Speak and adds its own method Bark. The program creates a Dog object and calls both methods.

csharp
using System;

class Animal
{
    public void Speak()
    {
        Console.WriteLine("Animal speaks");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Dog barks");
    }
}

class Program
{
    static void Main()
    {
        Dog myDog = new Dog();
        myDog.Speak();  // Inherited method
        myDog.Bark();   // Own method
    }
}
Output
Animal speaks Dog barks
⚠️

Common Pitfalls

Common mistakes when using inheritance in C# include:

  • Trying to inherit from sealed classes (which is not allowed).
  • Not using virtual and override keywords when you want to change base class behavior.
  • Accessing private members of the base class directly (only protected or public members are accessible).

Here is an example showing the wrong and right way to override a method:

csharp
class BaseClass
{
    public virtual void Show()
    {
        System.Console.WriteLine("Base Show");
    }
}

// Wrong: no override keyword
class DerivedWrong : BaseClass
{
    public void Show()  // This hides base method, does not override
    {
        System.Console.WriteLine("DerivedWrong Show");
    }
}

// Right: use override keyword
class DerivedRight : BaseClass
{
    public override void Show()
    {
        System.Console.WriteLine("DerivedRight Show");
    }
}
📊

Quick Reference

  • Inheritance syntax: class Derived : Base { }
  • Access modifiers: Use public or protected members to allow inheritance access.
  • Override methods: Use virtual in base and override in derived classes.
  • Sealed classes: Cannot be inherited.
  • Constructors: Base class constructors are called automatically or via base().

Key Takeaways

Use class Derived : Base to inherit members from a base class in C#.
Mark base class methods with virtual and override them in derived classes with override to change behavior.
Only public and protected members of the base class are accessible in derived classes.
Sealed classes cannot be inherited, so avoid trying to derive from them.
Use base class constructors explicitly with base() if needed.