I don t understand inheritance with base classes

0 votes
My knowledge of inheritance is rather rudimentary.

I want to design a base class that implements IDisposable and then let other classes that implement IDisposable inherit from it so that they all have the same base type.

It would enable me to group all of my belongings into a single list, allowing me to rid of everything at once when cleaning up. The problem is that in C#, a class can only inherit once and can only use interfaces otherwise.

Now, how may Thread-like classes inherit from my base class without me having to rewrite the Thread class from scratch?

It may sound silly, but in a nutshell, I want to allow existing classes to inherit a base class of my choice without losing their functionality. (So that all classes in a project share a similar basis that can be summed up into a single object type, such as List)

Is it even possible to do so?
Jun 11, 2022 in C# by krishna
• 2,820 points
836 views

1 answer to this question.

0 votes

If you just want to keep a list of objects that can be disposed of, you'll need a List<IDisposable>. Any class that implements that interface, either directly or indirectly, can be stored there.

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

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

I don't know if this is perfect either. Just a pointer.

answered Jun 17, 2022 by jyoti
• 1,240 points

Related Questions In C#

0 votes
0 answers

How can I split a string with a string delimiter

Suppose this is my string My name is ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
340 views
0 votes
1 answer

array of string with unknown size

Is there a specific reason why an ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
1,448 views
0 votes
1 answer

Which version of C# am I using

From developer cmd, type this csc -langversion:? This will ...READ MORE

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

Multiple Inheritance in C#

The.net Framework CLR and C# hasn't been ...READ MORE

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

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
657 views
0 votes
1 answer

OOP real world example

This is what I believe. OOP does not ...READ MORE

answered Jul 4, 2022 in C# by krishna
• 2,820 points
1,002 views
0 votes
1 answer

First C# program with two Classes. Confused

I figured out where your problem is Craps ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
431 views
0 votes
1 answer

When to use abstract classes

When you need a class for inheritance ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
568 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