Pages

Wednesday, 26 February 2014

Destructor in C# with Example

Destructor in C# with Example

Syntax:
 

class SampleA
{
    public SampleA()
    {
        // Constructor
    }
    ~SampleA()
    {
        // Destructor
    }
}


using System;
namespace ConsoleApplication3
{
    class SampleA
    {
        // Constructor
        public SampleA()
        {
            Console.WriteLine("An  Instance  Created");
        }
        // Destructor
        ~SampleA()
        {
            Console.WriteLine("An  Instance  Destroyed");
        }
    }

    class Program
    {
        public static void Test()
        {
            SampleA T = new SampleA(); // Created instance of class
        }
        static void Main(string[] args)
        {
            Test();
            GC.Collect();
            Console.ReadLine();
        }
    }
}

Output:


An instance created
An instance destroyed

No comments:

Post a Comment