Work Of Destructor in C# Language

Introduction

This is the special member function of a class, which is executed whenever an object of its class goes out of scope. Destructor is use to releasing resources before coming out the problem like closing files releasing memories. See in given below example.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace work_for_destructor_in_c_sarp

{

    class height

    {

        private double ht;

        public height()                              /*Create constructor */

        {

            Console.WriteLine("Object create");

        }

        ~height()                                    /*Create Destructor */

        {

            Console.WriteLine("Object is deleted");

        }

        public void set_height( double h)

        {

            ht = h;

        }

        public double get_height()

        {

            return ht;

        }

    }

    class Program

    {                      

        static void Main(string[] args)

        {

            height h1 = new height();                          /*create object */

 

            h1.set_height(4.5);                               /*call to function set height() */

 

            Console.WriteLine("Height is:" + h1.get_height());   /*call and print its value of function get height() */

        }

    }

}


Output:


output.jpg

Ebook Download
View all
Learn
View all