1

I have the following scenario. I subclassing and would like to know what happens to the superclass after the child class is gone

Following is my implementation:

class Program
{
    static void Main(string[] args)
    {
        using (var a = new SuperHuman())
        {

        }

        Console.ReadLine();
    }
}

class Human : IDisposable
{
    public string LoType = "Normal";

    public Human()
    {
        Console.WriteLine("Human Created");   
    }

    public string GetHumanType()
    {
        return LoType;
    }

    public void Dispose()
    {            
        Console.WriteLine("{0} Human Class gONE", LoType);            
    }
}

class SuperHuman : Human
{                
    public SuperHuman()           
    {
        LoType = "Super";
        Console.WriteLine("{0} Human Created",LoType);   

    }

}

gives me the following Output:

Human Created
SuperHuman Created
SuperHuman Class Gone

What I would like to know is if the Parent class is gone as well if not how do I dispose it along with the child class?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42
  • 4
    You don't dispose of a *class*, you dispose of an *object* - and when you create an instance of `SuperHuman`, you're not actually creating two objects... you're just creating one. So yes, your object has been disposed. – Jon Skeet May 29 '17 at 09:32
  • 2
    As a side note: one could say, this test output is not actually displaying creation and disposing, but which methods are called. The Superhuman constructor implicitly calls the Human constructor (which does not display the `LoType`). If the base class implemented a constructor with the LoType as parameter (or an abstract property), and the output would show that type, you would also only see the 'creation' of the superhuman class. Dispose only shows an output including the lotype and is not overridden in the inheriting class – Me.Name May 29 '17 at 09:46

2 Answers2

3

Just to be clear, the instance isn't gone once you dispose it. It has released all its unmanaged resources and it can be given free after that so the garbage collector can pick it up.

In your example code, there is just one instance (you don't have two instances for a derived class and its base class). You can't remove half an instance from the memory. It is there or it is gone.

The derived class' instance will be given free to the garbage collector after the end of the using block (not because of the using, but it leaves scope then). At the appropriate time, the garbage collector will free the memory.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

To your Question

Firstly there seems to be some confusion about the difference between classes and objects.

with the new-Keyword, a new Instance of SuperHuman is instantiated.

using (var a = new SuperHuman())
{
    // inside this block, "a" contains a reference to a SuperHuman-Object, 
    // which is also a Human-Object and thus can be Disposed. 
    // (As it implements IDisposable and must have a Dispose-Method)
}

So basically nothing is happening to your classes at all. The using Block is calling the Dispose()-Method on the Object at the end. So whatever you do inside this method is being done.

The IDisposable-Interface

Im not going to explain this to deep here.

This is the Description of the Interface from MSDN

Provides a mechanism for releasing unmanaged resources.

It means, that if you define a class Human implementing IDisposable, all unmanaged resources (eg. Database Connections) will be disposed upon a call to the Dispose()-Method. So you are responsible to free such resources in your code. However Human is still a managed resource. But this hasn't to do anything with IDisposable, as managed resources are being finalized by Garbage Collection, which automatically detects, when an object isn't in use anymore.

But what exactly are unmanaged resources?

This has already been answered here.

also free managed resources?

Even though the interface isn't meant to, you can also free managed resources by setting values to null. How this works is also already answered in another Thread.

Community
  • 1
  • 1
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • what If Human and Super Human classes have no parent child relation but instead I create a new instance of Human in SuperHuman Class then use the using statement even though at the end of the brackets SuperHuman class will be gone, what about the reference to Human class that is initialized inside the superhuman class? – Asım Gündüz May 29 '17 at 11:26
  • If class `A` deals with unmanaged resources, it shoud implement `IDisposable`. Then class `B` could *locally use* class `A` inside a `using`-Block and implicitly call `Dispose()` **or** *contain (association)* an instance of class `A` and itself implement `IDisposable` and call `A`-child's `Dispose()`-Method in its own `Dispose()`-Method. – LuckyLikey May 29 '17 at 11:36