A destructor is executed when an object goes out of scope. - CSharp Custom Type

CSharp examples for Custom Type:Destructor

Introduction

A destructor has the same name as the class with a prefixed tilde (~).

A destructor can neither return a value nor can it take any parameters.

Destructor is useful for releasing memory resources before exiting the program.

Destructors cannot be inherited or overloaded.

Demo Code

using System;//w ww . j av  a2s  .c o m
class Line {
   private double length;   // Length of a line
   public Line() {   // constructor
      Console.WriteLine("Object is being created");
   }
   ~Line() {   //destructor
      Console.WriteLine("Object is being deleted");
   }
   public void setLength( double len ) {
      length = len;
   }
   public double getLength() {
      return length;
   }
   static void Main(string[] args) {
      Line line = new Line();
      line.setLength(6.0);
      Console.WriteLine("Length of line : {0}", line.getLength());
   }
}

Result


Related Tutorials