Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 776 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Inheritance in C#

#1
Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

For instance I'm able to implement the missing multiple inheritance pattern using interfaces and three classes like that:

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

Every time I add a method to one of the interfaces I need to change the class *FirstAndSecond* as well.

Is there a way to inject multiple existing classes into one new class like it is possible in C++?

Maybe there is a solution using some kind of code generation?

Or it may look like this (imaginary c# syntax):

public class FirstAndSecond: IFirst from First, ISecond from Second
{ }

So that there won't be a need to update the class FirstAndSecond when I modify one of the interfaces.

---

### EDIT
Maybe it would be better to consider a practical example:

You have an existing class (e.g. a text based TCP client based on ITextTcpClient) which you do already use at different locations inside your project. Now you feel the need to create a component of your class to be easy accessible for windows forms developers.

As far as I know you currently have two ways to do this:

1. Write a new class that is inherited from components and implements the interface of the TextTcpClient class using an instance of the class itself as shown with FirstAndSecond.

2. Write a new class that inherits from TextTcpClient and somehow implements IComponent (haven't actually tried this yet).

In both cases you need to do work per method and not per class. Since you know that we will need all the methods of TextTcpClient and Component it would be the easiest solution to just combine those two into one class.

To avoid conflicts this may be done by code generation where the result could be altered afterwards but typing this by hand is a pure pain in the ass.
Reply

#2
You could have one abstract base class that implements both IFirst and ISecond, and then inherit from just that base.
Reply

#3
If you can live with the restriction that the methods of IFirst and ISecond must only interact with the contract of IFirst and ISecond (like in your example)... you can do what you ask with extension methods. In practice, this is rarely the case.

public interface IFirst {}
public interface ISecond {}

public class FirstAndSecond : IFirst, ISecond
{
}

public static MultipleInheritenceExtensions
{
public static void First(this IFirst theFirst)
{
Console.WriteLine("First");
}

public static void Second(this ISecond theSecond)
{
Console.WriteLine("Second");
}
}

///

public void Test()
{
FirstAndSecond fas = new FirstAndSecond();
fas.First();
fas.Second();
}

So the basic idea is that you define the required implementation in the interfaces... this required stuff should support the flexible implementation in the extension methods. Anytime you need to "add methods to the interface" instead you add an extension method.
Reply

#4
Multiple inheritance is one of those things that generally causes more problems than it solves. In C++ it fits the pattern of giving you enough rope to hang yourself, but Java and C# have chosen to go the safer route of not giving you the option. The biggest problem is what to do if you inherit multiple classes that have a method with the same signature that the inheritee doesn't implement. Which class's method should it choose? Or should that not compile? There is generally another way to implement most things that doesn't rely on multiple inheritance.
Reply

#5
Consider just using *[composition](

[To see links please register here]

)* instead of trying to simulate Multiple Inheritance. You can use Interfaces to define what classes make up the composition, eg: `ISteerable` implies a property of type `SteeringWheel`, `IBrakable` implies a property of type `BrakePedal`, etc.

Once you've done that, you could use the [Extension Methods](

[To see links please register here]

) feature added to C# 3.0 to further simplify calling methods on those implied properties, eg:

public interface ISteerable { SteeringWheel wheel { get; set; } }

public interface IBrakable { BrakePedal brake { get; set; } }

public class Vehicle : ISteerable, IBrakable
{
public SteeringWheel wheel { get; set; }

public BrakePedal brake { get; set; }

public Vehicle() { wheel = new SteeringWheel(); brake = new BrakePedal(); }
}

public static class SteeringExtensions
{
public static void SteerLeft(this ISteerable vehicle)
{
vehicle.wheel.SteerLeft();
}
}

public static class BrakeExtensions
{
public static void Stop(this IBrakable vehicle)
{
vehicle.brake.ApplyUntilStop();
}
}


public class Main
{
Vehicle myCar = new Vehicle();

public void main()
{
myCar.SteerLeft();
myCar.Stop();
}
}
Reply

#6
If X inherits from Y, that has two somewhat orthogonal effects:
<ol>
<li>Y will provide default functionality for X, so the code for X only has to include stuff which is different from Y.
<li>Almost anyplace a Y would be expected, an X may be used instead.
</ol>
Although inheritance provides for both features, it is not hard to imagine circumstances where either could be of use without the other. No .net language I know of has a direct way of implementing the first without the second, though one could obtain such functionality by defining a base class which is never used directly, and having one or more classes that inherit directly from it without adding anything new (such classes could share all their code, but would not be substitutable for each other). Any CLR-compliant language, however, will allow the use of interfaces which provide the second feature of interfaces (substitutability) without the first (member reuse).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through