-1

Hi, As we know that C# does not support multiple inheritances in classes.

But I need to do add the feature of both ClassA and ClassB in MyClass:

  public class ClassA
{
    public long Account_ID { get; set; }
}
public class ClassB
{
    public string Name { get; set; }
}
// I want to  achieve Something like this
public class ClassC : ClassA, ClassB
{
    Public String AccountName {Get;Set;}
    // To do Other Stuff
}
  

I am getting This error:
C# Class cannot have multiple base classes: and

Is there any way I can achieve this?

Thank you very much :)

Dhanraj Kumar
  • 31
  • 2
  • 6
  • 2
    You cant do that in C#. Only one class, multiple interfaces. Use composition, not inheritance. – Divisadero Jan 15 '21 at 11:58
  • 1
    See [C# | Multiple inheritance using interfaces](https://www.geeksforgeeks.org/c-sharp-multiple-inheritance-using-interfaces/) and [Implement Multiple Inheritance In C#](https://www.c-sharpcorner.com/article/implement-multiple-inheritance-in-c-sharp2/) and [Simulating Multiple Inheritance In C#](http://benbowen.blog/post/simulating_multiple_inheritance_in_csharp/) –  Jan 15 '21 at 11:59
  • 1
    You can use [composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance) to [bypass the lack of multiple inheritance](https://www.c-sharpcorner.com/UploadFile/ff2f08/inheritance-vs-composition/) instead of using interfaces. –  Jan 15 '21 at 12:00
  • IRT everyone repeating "composition over inheritance" without applying critical thinking: remember that C# does not support interface-forwarding nor true mixins, nor can you override implementations when composing. **It's not that simple!**. – Dai Jan 15 '21 at 12:05
  • @Dai, no it's not that simple, but seeing as what is asked for is not possible, one **must** use alternatives. – Lasse V. Karlsen Jan 15 '21 at 12:06
  • Why do you want multiple inheritance in the first place? There's no `is-a` relation between the classes you posted, so even in languages that allow multiple inheritance, it would be the wrong choice – Panagiotis Kanavos Jan 15 '21 at 12:06
  • A specific kind of mixing, traits, is offered by C# 8's default interface members. Same as Java and PHP, which don't provide multiple inheritance either. There are other mechanisms too, each meant to solve a specific problem. – Panagiotis Kanavos Jan 15 '21 at 12:09
  • 2
    What is the *actual* problem in this question? Why would a class get a *storage location* for ID from one parent and another storage location from a different parent? Why not interfaces? The details *matter* – Panagiotis Kanavos Jan 15 '21 at 12:10

1 Answers1

0

Not allowing multiple inheritance is a well-known design in C#, as well as in Java. Instead, you could use interface for polymorphism. A class is allowed to implement multiple interface. BTW, you should google "composition over inheritance" as a best practice.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alsein
  • 4,268
  • 1
  • 15
  • 35
  • Thank you for taking the time to share your knowledge & experience. But your answer has a low quality. Sometimes a comment or a flag/close may be more relevant. To help you improve your answersplease read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), [How do I write a good answer to a question](https://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question) & [Answering technical questions helpfully](https://codeblog.jonskeet.uk/2009/02/17/answering-technical-questions-helpfully). –  Jan 15 '21 at 12:15