What is the difference between “dispose” and “finalize” variables in C#?
What is the difference between “dispose” and “finalize” variables in C#?
Collapse
X
-
Tags: None
-
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 calledDispose()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.)
TheFinalizemethod 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