?

Log in

Previous Entry | Next Entry

We need to follow the below steps:-

  • Implement IDisposable interface and implement the dispose function.

  • In Dispose function calls the “GC.SuppressFinalize” method.

  • At the client side ensure that the “Dispose” function is called when the object is no more required.

Below goes the code, this is also called as “Finalize and Dispose pattern”. This ensures that your objects are created in Generation 0 rather than Generation 1. “GC.SuppressFinalize” tells the garbage collector to not worry about destructor and destroy the objects in the first call itself.

 class clsMyClass : IDisposable
    {
        ~clsMyClass()
        {
            // In case the client forgets to call
            // Dispose , destructor will be invoked for
            Dispose(false);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Free managed objects.
            }
            // Free unmanaged objects
        }

        public void Dispose()
        {
            Dispose(true);
            // Ensure that the destructor is not called
            GC.SuppressFinalize(this);
        }
    }

Also see our .NET interview questions video on Garbage Collector, Gen 0, 1 & 2:  -

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Tags

Powered by LiveJournal.com