Home > C# > Inheritance in C#

Inheritance in C#

Inheritance is a very important aspect of Object Oriented Programming. It allows a class to inherit all the properties of another class. Let’s discuss this topic in detail.

What is Inheritance?

Inheritance is one of the most important pillars of Object Oriented Programming. Inheritance allows a class to access all the property of the Base class and also Base class can have its own behaviour. Using Inheritance a class can be act as a template for other classes. A class that allows inheritance is called as Base Class and the class that inherits other class is called as derived class.

Real World Example of Inheritance:

An example of Inheritance that exists in the real world is Shape class, which is a base class and its derived class can be Triangle, Rectangle and Square.

The basic concept of Inheritance is around IS-A relationship. For Example, Triangle is a Shape.


When should we use Inheritance?

I think we got some basic knowledge of Inheritance with an example. Now the point is when we should use Inheritance. As I told you the idea of Inheritance is around IS-A relationship. There are two types of relationships between classes:

  • IS-A Relationship.
  • HAS-A Relationship

Inheritance is used when your classes have IS-A Relationship. Such as Triangle IS-A Shape. A Rectangle is a Shape. Suppose I have a class that calculates the area of the shape then the relationship between the Shape and ShapeArea will be HAS-A.


Why should we use Inheritance:

Inheritance is a way to achieve the functionality of one class without creating a new class. It reduces the complexity of the code and eliminates the repetitive code. With Inheritance, it becomes to maintain our code.

An example of Inheritance in C#:

Open Visual Studio and Create a Console Application Project.


Open Program.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InheritanceDemo
class Program
class Shape
public double dSideA;
public double dSideB;

public void ShapeDimensions()
Console.WriteLine("Dimensions of the shape are " +
dSideA + " and " + dSideB);
// Triangle is derived from Shape. Both have IS-A relationship
class Triangle : Shape
public void Area()
Console.WriteLine("Area of the Triangle Is :" + dSideA * dSideB / 2);
class Rectangle : Shape
public void Area()
Console.WriteLine("Area of the Rectagle Is :" + dSideA * dSideB);

static void Main(string[] args)
Triangle objTriangle = new Triangle();
objTriangle.dSideA = 10;
objTriangle.dSideB = 20;
Rectangle objRectangle = new Rectangle();
objRectangle.dSideA = 40;
objRectangle.dSideB = 50;


Run the application and look over the output window.


You can download the complete source code for the application from here.

Hope you understand the concept of Inheritance in C#. We will create more example on Inheritance in later sessions.

View More:

Thank You.


  1. That is the precise weblog for anybody who desires to search out out about this topic. You notice a lot its almost hard to argue with you (not that I actually would want匟aHa). You positively put a new spin on a topic thats been written about for years. Nice stuff, just great!

  2. hello!,I like your writing so much! percentage we be in contact more approximately your post on AOL? I need an expert in this area to solve my problem. Maybe that’s you! Taking a look ahead to see you.

Leave a Reply

Your email address will not be published.