You must Sign In to post a response.
  • Category: .NET

    What is the difference between dispose and finalize (destructor)

    hi,


    can any one briefly explain what is the difference between dispose() and
    finalize() in vb.net and c#.net

    can we use both of them?








    from
    mausumi
  • #674549
    The destructor are like constructor, called automatically before obhect get's destroy, It is important to note that the destructors are also called Finalizers in C# destcuctor is called as Finalize method. Called after TRY...CATCH block.

    The IDisposable interface contains a method Dispose(), we can freeing up of unmanaged resources using Dispose() method.
    we can impelemnt IDisposable intgerface to any class
    here is code


    public class Channel : IDisposable


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #674552
    Hi,

    The garbage collector reclaims object memory when there are no valid references left for the object by calling Finalize method.

    But we as a programmer might want to release some objects from memory like database connections explicitly, if no more processing is left with those objects. So instead of depending upon garbage collector to reclaim that memory (which is generally expensive in terms of size) , we explicitly release those resources by calling Dispose method.

    Hope it'll help you.
    Regards
    Ajatshatru

  • #674561
    Hi,
    Unmanaged resources are released by using Dispose method by developers.Once we are finished with expensive resources such as windows handles, database connection, file system, we can call Dispose method instead of depending up on garbage collector.
    Finalize is called from the garbage collector if dispose is not called. The garbage collector calls this method only if there are no valid refernces to any particular object.

    I hope it will help you.

    Regards,
    Prachi.

  • #674564
    Finalize():
    Finalization is the process by which the GC allows objects to clean up any unmanaged resources that they're holding, before the actually destroying the instance.

    Dispose():
    Dispose method called explicitly on any object that implements it to free any unmanaged resources for which the object may be holding references. The Dispose method generally doesn't free managed memory.

  • #674689
    Finalize method will be automatically called by the garbage collector to clear the unused memory and dispose method can be called explicitly to free the unused memory.

    Thanks & Regards


    Bunty

  • #674839
    I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you simultaneously define both methods in a class.

    For example, the following code fragment:

    class TestFinalize
    {
    ~TestFinalize()
    {
    Finalize();
    }

    public bool Finalize()
    {
    return true;
    }
    }

    gives the following error on the call to Finalize in the destructor:

    The call is ambiguous between the following methods or properties:
    'TestFinalize.~TestFinalize()' and 'TestFinalize.Finalize()'

    and if the call to Finalize is commented out, it gives the following error:

    Type 'ManagementConcepts.Service.TestFinalize' already defines a member called
    'Finalize' with the same parameter types


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.