Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

c# - 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?

question from:https://stackoverflow.com/questions/65934031/i-dont-understand-inheritance-with-base-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...