Destructor In C#

Now we are going to learn about destructor in C#.
  • 2484
Introduction

Destructors is a process of removing unusable objects by using the garbage collection. Destructors are used to destruct instances of classes. Destructors have some condition for there use as like we will use only single destructor in a class.

Destructor will not use as a inherited or overloaded and it will be invoked automatically we cannot call destructors.

Example

class ABC
{
 ~ ABC ()
{
 System.Diagnostics.Trace.WriteLine("ABC's destructor is called.");
}
}
 class DEF : ABC 
{
 ~ DEF ()
{
 System.Diagnostics.Trace.WriteLine("DEF's destructor is called.");
}
}
class XYZ : DEF
{
~XYZ ()
{
System.Diagnostics.Trace.WriteLine("XYZ's destructor is called.");
}
}
class TestDestructors
{
static void Main()
{
 XYZ x=new XYZ ();
}
}
}

© 2020 DotNetHeaven. All rights reserved.