What is multiple inheritance in C#?

PHPz
Release: 2023-09-04 17:05:02
forward
1249 people have browsed it

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();
}
Copy after login

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

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

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!");
      }
   }
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!