Multiple Inheritance
Tags: csharp, dotnet, programming
Published: 2023-12-14 13:42:00

##### Multiple Inheritance in C#

- [Multiple Inheritance in C#](#multiple-inheritance-in-c)
- [Introduction](#introduction)
- [Example](#example)
- [Summary](#summary)
- [References](#references)

## Introduction
This article explains the concept of multiple inheritance in C#. It is a concept of deriving a class from more than one base class.


**What is Inheritance?**
_Inheritance is one of the key features of Object Oriented Programming. It is a mechanism in which one class acquires the property of another class._

:warning: **Note:** C# does not support multiple inheritance on classes. However, it supports multiple inheritance on interfaces.

## Setup
```bash
mkdir MultipleInheritance
cd MultipleInheritance
dotnet new console
```

In the `Program.cs` file, add the following code:
```csharp
namespace MultipleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            student.FirstName = "John";
            student.LastName = "Doe";
            student.CourseName = "Programming with C#";
            student.CourseCode = "C#";
            Console.WriteLine($"Student {student.FirstName} {student.LastName} is enrolled in {student.CourseName} ({student.CourseCode})");

            Teacher teacher = new Teacher();
            teacher.CourseName = "Programming with C#";
            teacher.CourseCode = "C#";

            Console.WriteLine($"Teacher teaches {teacher.CourseName} ({teacher.CourseCode})");
        }

        public interface IPerson
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }

        }

        //ICourse
        public interface ICourse
        {
            public string CourseName { get; set; }
            public string CourseCode { get; set; }
        }

        //Student
        // we cannot inherit from multiple classes but we can inherit from multiple interfaces
        public class Student : IPerson, ICourse
        {
            public string FirstName { get; set; } = string.Empty;
            public string LastName { get; set; } = string.Empty;
            public string CourseName { get; set; } = string.Empty;
            public string CourseCode { get; set; } = string.Empty;
        }

        //Teacher
        public class Teacher : ICourse
        {
            public string CourseName { get; set; } = string.Empty;
            public string CourseCode { get; set; } = string.Empty;
        }
    }
}
```

Explanation:
- We have created two interfaces `IPerson` and `ICourse`.
- We have created a class `Student` which implements both the interfaces `IPerson` and `ICourse`.
- We have created an object of the class `Student` and assigned values to its properties.
- We have printed the values of the properties of the object.

#### Example II
```csharp
namespace MultipleInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();
            user.FirstName = "John";
            user.LastName = "Doe";
            user.Email = "a2b.com";

            Console.WriteLine($"User {user.FirstName} {user.LastName} has email {user.Email}, created at {user.CreatedAt} and updated at {user.UpdatedAt}");
        }

        public interface IBaseEntity
        {
            public int Id { get; set; } 
            public DateTime CreatedAt { get; set; } 
            public DateTime UpdatedAt { get; set; } 
        }

        public interface  IUser : IBaseEntity
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Email { get; set; }
            public string Password { get; set; }
        }

        public class User : IUser
        {
            public int Id { get; set; }
            public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
            public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
            public string FirstName { get; set; } = string.Empty;
            public string LastName { get; set; } = string.Empty;
            public string Email { get; set; } = string.Empty;
            public string Password { get; set; } = string.Empty;
        }


    }
}
```

## Summary
- Multiple inheritance on classes is not supported in C#.
- Multiple inheritance on interfaces is supported in C#.

## References
- [Multiple Inheritance in C#](https://www.c-sharpcorner.com/article/multiple-inheritance-in-C-Sharp/)