What is the difference between “dispose” and “finalize” variables in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VamshiKrishna14
    New Member
    • May 2017
    • 1

    What is the difference between “dispose” and “finalize” variables in C#?

    What is the difference between “dispose” and “finalize” variables in C#?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Dispose and Finalize are not variables, they are Methods that are called to help clean up your objects when your application is finished using them.

    The difference between them comes down to the way garbage collection works.

    The garbage collector can only release/clean up memory for managed code (managed code is .NET components) and cannot release any memory used by unmanaged code (like file streams, database connections, network sockets, window handlers etc.)

    If your application uses any unmanaged components, you should implement the IDisposable Interface (see link for details). The IDisposable interface exposes a method called Dispose() that is used to release any unmanaged components...me aning when you write code for this method you would properly release the unmanaged components from memory (close open file streams or network connections or database connections etc.)



    The Finalize method is the class's [icode]Deconstructor[/url]. You cannot call this method and it is used by the garbage collector to deconstruct the class and clean up managed components.

    Comment

    Working...