what is destructor in c# net with example ?

Destructor  is special methods that are used to release the instance of a class from memory. A class can have only one destructor, A destructor has the same name as its class but is prefixed with a ~ (tilde)

For example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace destructors
{
    class calculator
    {
        static int num1, numr2, total;
        public void add()
        {
            total = num1 + num2;
            Console.WriteLine("the result is {0}", total);
        }
        calculator()
        {
            num1 = 20;
            numr2 = 30;
            total = 0;
            Console.WriteLine("constructor invoked");
        }
        ~calculator()
        {
            Console.WriteLine("destructor invoked");
        }
        static void Main(string[] args)
        {
            calculator cl = new calculator();
            cl.addnum();
            Console.ReadLine();
        }
    }

}
what is destructor in c# net with example ? what is destructor in c# net with example ? Reviewed by NEERAJ SRIVASTAVA on 6:08:00 PM Rating: 5

No comments:

Powered by Blogger.