7
Reply

What is the difference between Finalize() and Dispose() methods?

Gautam Kumar

Gautam Kumar

10y
36.9k
0
Reply

    Dispose It belongs to IDisposable interface. and internal called by user code.Finalize It belongs to Object class. and It's implemented with the help of destructor in C#

    Dispose() is called when we want for an object to release any unmanaged resources with them. On the other hand Finalize() is used for the same purpose but it doesn't assure the garbage collection of an object.

    .NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. - It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary.- At runtime C#, C++ destructors are automatically Converted to Finalize method. But in VB.NET you need to override Finalize method, since it does not support destructor.- You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.- A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.

    You can find differences here. http://dotnetmagics.com/difference-between-finalize-and-dispose/

    Finalize is used internally by garbage collector Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed. but Dispose() is used by used externally .........

    Finalize is used internally by garbage collector Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed. but Dispose() is used by used externally

    Finalize:Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed.Internally, it is called by Garbage Collector and cannot be called by user code.It belongs to Object class.Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.There is performance costs associated with Finalize methodDispose:It is used to free unmanaged resources like files, database connections, COM etc. at any time.Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface.It belongs to IDisposable interface.Implement this when you are writing a custom class that will be used by other users.There is no performance costs associated with Dispose method.For similar kind of c# interview questions you can refer this link c# interview questions