Multiple Inheritance in C

0 votes

C# does not directly provide such a pattern since multiple inheritance is bad (it makes the code more difficult). However, having this skill would be useful at times.

For example, I can use interfaces and three classes to implement the missing multiple inheritance pattern:

public interface IFirst { void FirstMethod(); }
public interface ISecond { void SecondMethod(); }

public class First:IFirst 
{ 
    public void FirstMethod() { Console.WriteLine("First"); } 
}

public class Second:ISecond 
{ 
    public void SecondMethod() { Console.WriteLine("Second"); } 
}

public class FirstAndSecond: IFirst, ISecond
{
    First first = new First();
    Second second = new Second();
    public void FirstMethod() { first.FirstMethod(); }
    public void SecondMethod() { second.SecondMethod(); }
}

I have to update the class FirstAndSecond every time I add a method to one of the interfaces.

Is it possible, as in C++, to inject numerous existing classes into a single new class?

Is there a way to solve this problem with code generation?

Alternatively, it may look something like this (imaginary c# syntax):

public class FirstAndSecond: IFirst from First, ISecond from Second
{ }
Jun 7 in C# by pranav
• 2,320 points
13 views

1 answer to this question.

0 votes
The.net Framework CLR and C# hasn't been implemented MI because they haven't figured out how it would work with C#, VB.net, and the other languages, not because "it would make source more complex."

MI is a valuable notion; nevertheless, there are still some unanswered concerns, such as: "What do you do when there are numerous shared base classes in various superclasses?"

MI only works and works well in Perl, which is the only language I've ever worked with. Net may introduce it in the future, but not now. The CLR already supports MI, but as I previously stated, there are no language structures for it beyond that.

Instead, you'll have to make do with proxy objects and different interfaces till then.
answered Jun 7 by rajiv
• 1,620 points

Related Questions In C#

0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7 in C# by pranav
• 2,320 points
12 views
0 votes
1 answer

Print Pdf in C#

Using an installed Adobe Reader or any ...READ MORE

answered Jun 7 in C# by pranav
• 2,320 points
15 views
0 votes
1 answer

Why should I avoid using Properties in C#?

Jeff dislikes properties because they resemble fields, ...READ MORE

answered Jun 7 in C# by rajiv
• 1,620 points
20 views
0 votes
1 answer

Datatypes in C#

You might be beginner I think. Go ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
11 views
0 votes
1 answer

I don't understand inheritance with base classes

If you just want to keep a ...READ MORE

answered Jun 17 in C# by jyoti
• 1,240 points
8 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,100 points
4,468 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
499 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
7,252 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
21 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP