Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand inheritance with base classes

My understanding of inheritance is pretty basic.

I want to create a base class, which implements IDisposable and then let other classes with functionality inherit from that base class, so that they share a base type.

It would allow me to put every object into one list, so that I can dispose of everything in one go, when I want to clean up. The thing is, a class in C# can only inherit once and otherwise only use interfaces.

Now, how can classes like Thread, inherit from my base class without me having to basically code the Thread class all over again?

It may sound dumb, but in short: I want to let already existing classes inherit a base class of my choosing without loosing its functionality. (So that all classes used in a project have a common base, to be summed up into one object type, for like List)

Is that even possible?

like image 207
Rakku Avatar asked Oct 30 '25 15:10

Rakku


2 Answers

If your aim is to simply store a list of objects which can be disposed, then you want a List<IDisposable>. Any class which, directly or indirectly implements that interface can be stored therein.

var disposables = new List<IDisposable>();
disposables.Add(new MyClass1());
disposables.Add(new SomeClass());
disposables.Add(new AnotherClass());

disposables.ForEach(d => d.Dispose());

Whether this is a worthwhle pattern, I'm not convinced!

like image 173
Jamiec Avatar answered Nov 01 '25 04:11

Jamiec


Example you given invlolves interface IDisposable. In C#, class can implement multiple interfaces.

On the other hand, in C# class may inherit from exactly one class.

Having said that, it is not always good idea to inherit from classes, as it very limiting. Instead I would suggest composition: wiki

For example, I would suggest something like composition design pattern (it is a small variation, as the composite does not have collection of objects, rather just one object):

public interface IFoo
{
    void DoFooStuff();
}

public class Foo : IFoo
{
    public void DoFooStuff() {...};
}

public class ShouldImplementFoo : IFoo
{
    // Composition, you need to initialize it some time
    private Foo _foo;
    
    public void DoFooStuff() { _foo.DoFooStuff(); }
}

EDIT

After rethinking your problem, you seem to want to put some set of objects (of different classes) in a list, so you can execute some defined action on each object in a list.

The simpliest is: define interface with your defined action:

public interface IMyIntf
{
    void MyDesiredAction();
}

And now, make all classes implement it. Now you might think "Well, now I need to implement this method everywhere..." - nothing like that! Just use adapter design pattern, for example, having class:

public class MyConcreteClass
{
    public void ActionIWishToExecuteOnThisObject() 
    { ... }
}

you just modify it to:

public class MyConcreteClass : IMyIntf
{
    public void ActionIWishToExecuteOnThisObject() 
    { ... }

    // implement interface
    public void MyDesiredAction()
    {
        ActionIWishToExecuteOnThisObject();
    }
}

Now, you can use some aggregate, like List<IMyIntf>, place all objects you are interested in there and do:

foreach(var item in myList)
    item.MyDesiredAction();
like image 37
Michał Turczyn Avatar answered Nov 01 '25 05:11

Michał Turczyn