IDisposable pattern.

IDisposable pattern.

Finalize - implement of finalize method in our class to release the unmanaged resources or objects. Managed objects are object that are developed in .Net . But Unmamanged resources are one which are

like windows componenet . So to release the unmanaged resource Finalize should be implemented. But we cannot access the Finalize Method and this will be called by the Garbage Collector.

When an object is created it will be in GENERATION1 .GC comes to play and check if some object is unused /inactive then GC will released and deallocate the memory. GC will check the object header and

check to see if there is any finalize method mentioned there or not . If its there then it will be moved to Finalized Queue which is also managed by the GC.But when the GC will go and check the Finalized

queue to released the memeory is not known and we have no control to it.
The Finalize is implemented by using the destructor of the class.The descrutor will get converted to Finalize method .It overrides the Finalze method of the base object.Using Destructor we implement the

finalize

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManualResetEvent_
{
    class A
    {
        public A()
        {
            Console.WriteLine("\t\t\tConstructor called from A");
        }

        ~A()
        {
            Console.WriteLine("\t\t\tDesctructor called from A");
        }
    }

    class B : A
    {
        public B()
        {
            Console.WriteLine("\t\t\tConstructor called from B");
        }

        ~B()
        {
            Console.WriteLine("\t\t\tDestructor called from B");
        }
    }

    class C : B
    {
        public C()
        {
            Console.WriteLine("\t\t\tConstructor called from C");
        }
        ~C()
        {
            Console.WriteLine("\t\t\tDestructor called from C");
        }
    }

    public class main_run
    {
        public static void Main()
        {
            C c = new C();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\t\t\tNew Object is created");
            c = null;
           
            Console.ForegroundColor=ConsoleColor.Red;
            Console.WriteLine("\t\t\tObject will be destroyed");
            GC.Collect();
            Console.ReadLine();

        }
    }
}


So we have disposable pattern to solve the uncertainity in GC process to trigger and remove the objects from the Finalize queue.

We hav Idisposable interface which has dispose method , which manages this scenerio. So the Dispose method will be used to clean up the unmanaged resource.

Using Block{} is used to free up the unmanaged resources (like DB connections etc) and should have dispose menthod implements.If we do not use the using block then dispose menthod should be called

explicitly. Finalize should be used only in case if unmanaged resource .In Dispose menthod the finalize method is suppressed.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManualResetEvent_
{
 public class DisposableExample : IDisposable
    {
        private bool disposed = false;      //A boolean variable is made false
        public void greeting(string name)
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("Hello {0}", name);
        }

        ~DisposableExample()
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine(" destructor or finalized was called");
        }

        public void Dispose()                //It is the user reponsobility to call the dispose method
        {                                               //if  the user forgets to call the dispose menthod then
            Dispose(true);                       //as a back up we called the finalize menthod is also
            GC.SuppressFinalize(this);     //implemented.
        }
//=======================Disposable Pattern ==================================//
        public void Dispose(bool dispose)
        {
            if(!disposed)
            {
                if(dispose)
                {
                    //code to freeup the resource managed resource
                    Console.WriteLine("\t\t\tManaged object destroyed");
                }
                else
                    //code to freeup the unmanaged resources
                    Console.WriteLine("\t\t\tUnManaged object destroyed");
                    dispose=true;
            }
        }
        }
       
//=======================Disposable Pattern =======================================//


    public class main_run
    {
        public static void Main()
        {
            DisposableExample c = new DisposableExample();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\t\t\tNew Object is created");
            c.Dispose();
            c = null;
            Console.ForegroundColor=ConsoleColor.Red;
            Console.WriteLine("\t\t\tObject will be destroyed");
            Console.ReadLine();

        }
    }
}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6