Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A

.NET interview questions: - How to maintain performance and can clean unmanaged objects?

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
            // In case the client forgets to call
            // Dispose , destructor will be invoked for
        protected virtual void Dispose(bool disposing)
            if (disposing)
                // Free managed objects.
            // Free unmanaged objects

        public void Dispose()
            // Ensure that the destructor is not called

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

Tags: .net, .net interview questions, csharp interview questions, garbage collector, it interview questions with answers, manage unmanage code, programming interview questions

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded