Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

calling destructor of base class without calling destructor of derived class in c# ,.net

can we call a destructor of base class without calling destructor of derived class
using System;
class A
{
 public A()
 {
  Console.WriteLine("Creating A");
 }
 ~A()
 {
  Console.WriteLine("Destroying A");
 }
}
 
class B:A
{
 public B()
 {
  Console.WriteLine("Creating B");
 }
 ~B()
 {
  Console.WriteLine("Destroying B");
 }
 
}
class C:B
{
 public C()
 {
  Console.WriteLine("Creating C");
 }
 
 ~C()
 {
  Console.WriteLine("Destroying C");
 }
}
class App
{
 public static void Main()
 {
  C c=new C();
  Console.WriteLine("Object Created ");
  Console.WriteLine("Press enter to Destroy it");
  Console.ReadLine();
  c=null;
  //GC.Collect();
  Console.Read();
 }
 
}

Open in new window

0
Saini_Vikas
Asked:
Saini_Vikas
  • 2
1 Solution
 
Richard LeeSoftware EnthusiastCommented:
Calling the destructor of a derived class without calling the destructor of the base class would be incorrect. If a class is built from another house if you destroy it then everything should be destroyed.

If you want another behaviour then you need to consider other option of Object Oriented programming such as Aggregation or Composition.
0
 
Saini_VikasAuthor Commented:
Hi ,
Please providesolution  with some satisfactory answer.
0
 
Richard LeeSoftware EnthusiastCommented:
50 points do not warrant that. Please ask the question and provide the person answering some incentive,
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now