Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many classes can you inherit from in C#?

Tags:

How many classes can you inherit from in .NET?

There are several backend C# files that I would like to share separate static methods, but they are all in different classes. Is there a way to inherit multiple classes?

Example code would be much appreciated.

like image 928
locoboy Avatar asked Aug 30 '10 13:08

locoboy


People also ask

How many classes can you inherit?

Although classes can inherit only one class, they can implement multiple interfaces.

Can a class inherit from multiple classes C?

Multiple Inheritance in C++ Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited.

Can u inherit from multiple classes?

Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass.

How many classes can a single class inherit from?

1 Answer. The best explanation: Any class can inherit any number of classes. There is no limit defined for the number of classes being inherited by a single class.


2 Answers

C# does not support multiple inheritance (meaning a single class inherits from multiple classes). You can, however, implement multiple interfaces in a single class.

As far as chaining together inherited classes, there isn't a limit per-se. Just keep in mind the complexity you will introduce to your system. When using inheritance, make sure you are using it in a "is a" scenario. A cheeta is an animal. A mazda is a car. Otherwise, your inheritance tightly couples your classes with a design that becomes much more difficult to maintain.

If they are static "utility" methods, just invoke them directly without inheriting. Unless those methods belong to the entity you are creating, you should not use inheritance.

like image 192
Jordan Parmer Avatar answered Oct 05 '22 13:10

Jordan Parmer


You can only inherit from a single class. It is however possible to implement multiple interfaces.

like image 32
Tom Vervoort Avatar answered Oct 05 '22 13:10

Tom Vervoort