Solved

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

Posted on 2009-07-06
3
186 Views
Last Modified: 2012-05-07
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
Comment
Question by:Saini_Vikas
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 50 total points
Comment Utility
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
 

Author Closing Comment

by:Saini_Vikas
Comment Utility
Hi ,
Please providesolution  with some satisfactory answer.
0
 
LVL 18

Expert Comment

by:Richard Lee
Comment Utility
50 points do not warrant that. Please ask the question and provide the person answering some incentive,
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now