Home  >  Article  >  Backend Development  >  What is multiple inheritance in C#?

What is multiple inheritance in C#?

PHPz
PHPzforward
2023-09-04 17:05:021292browse

C# 中的多重继承是什么?

C# does not support the use of multiple inheritance, but it can be implemented using interfaces.

The following is the implementation using interface inheritance. Create two interfaces-

public interface BaseOne {
   void display();
}
public interface BaseTwo {
   void display();
}

Now set up the interface like setting up a derived class,

public class ChildOne : BaseOne, BaseTwo {
   public void display() {
      Console.WriteLine("Child Class!");
   }
}

We will call the subclass function as shown in the following code to implement multiple inheritance in C#-

Example

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

namespace Demo {
   class Program {
      static void Main(string[] args) {
         ChildOne c = new ChildOne();
         c.display();
         Console.ReadKey();
      }
   }

   public interface BaseOne {
      void display();
   }

   public interface BaseTwo {
      void display();
   }

   public class ChildOne : BaseOne, BaseTwo {
      public void display() {
         Console.WriteLine("Child Class!");
      }
   }
}

The above is the detailed content of What is multiple inheritance in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete