Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-07-06
3
Medium Priority
?
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 100 total points
ID: 24787799
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
ID: 31618561
Hi ,
Please providesolution  with some satisfactory answer.
0
 
LVL 18

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

718 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