Constructor and Destructor

C# Example For Constructor and Destructor:

class ClassA
{
    public ClassA()
    {
        Console.WriteLine("Creating ClassA");
    }
    ~ClassA()
    {
        Console.WriteLine("Destroying ClassA");
    }
}

class ClassB : ClassA
{
    public ClassB()
    {
        Console.WriteLine("Creating ClassB");
    }
    ~ClassB()
    {
        Console.WriteLine("Destroying ClassB");
    }

}
class ClassC : ClassB
{
    public ClassC()
    {
        Console.WriteLine("Creating ClassC");
    }

    ~ClassC()
    {
        Console.WriteLine("Destroying ClassC");
    }
}
class Mehul
{
    public static void Main()
    {
        ClassC c = new ClassC();
        Console.WriteLine("Press enter to Destroy Object");
        Console.ReadLine();
        c = null;
        GC.Collect();
        Console.ReadLine();
    }

}

The Output of above will be:

 

1 comment:

  1. good one mehul..it will be good if u print the values also

    ReplyDelete