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, 2022 in C# by pranav
• 2,590 points
566 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, 2022 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, 2022 in C# by pranav
• 2,590 points
856 views
0 votes
1 answer

Print Pdf in C#

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

answered Jun 7, 2022 in C# by pranav
• 2,590 points
829 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, 2022 in C# by rajiv
• 1,620 points
651 views
0 votes
1 answer

Datatypes in C#

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

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
538 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, 2022 in C# by jyoti
• 1,240 points
837 views
0 votes
0 answers

Use of interfaces, practical and real world example

I've been attempting to figure out what ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
1,140 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,140 points
5,716 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
1,109 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, 2022 in C# by rajiv
• 1,620 points
698 views
0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
475 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP