TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Dotnet framework difference faqs- 2
1. 1.Difference between Finalize() and Dispose() methods in .NET
S.No Finalize() Dispose()
1 Finalize() belongs to the Object Dispose() belongs to the IDisposable
class. interface
2 It is automatically called by the We have to manually write the code to
Garbage Collection mechanism implement it(User Code)
when the object goes out of the ex: if we have emp class we have to
scope(usually at the end of the inherit it from the IDisposable
program) interface
and write code. We may have to
suppress the Finalize method using
GC.SuppressFinalize() method.
3 It is slower method and not Faster method for instant disposal of
suitable for instant disposing of the objects.
the objects.
Example: user interface Controls.
Example: Forms, SqlConnection class have built
in implementaion of Dispose method.
class employee
{
//This is the destructor of emp
class
~employee()
{
}
//This destructor is implicitly
compiled to the Finalize method.
}
4 It is non-deterministic function It is deterministic function as
i.e., it is uncertain when Garbage Dispose() method is explicitly called
Collector will call Finalize() by the User Code.
method to reclaim memory.
2.Difference between Dispose and Destructor in .NET
S.No Dispose Destructor
1 Unmanaged resources are It is used to release unused managed
removed by dispose method and it resources and it is called automatically
is called manually by the Garbage Collector
2 It is used to remove the unused It is used to de-allocate the allocated
resources from the memory. memory by the constructor method
2. 3.Difference between Close() and Dispose() methods in .NET
S.No Close() Dispose()
1 When a Close() method is Where as Dispose() method
called,database connection will be permanently close and removes
temporarily closed and can be connection object from memory and
opened once again using Open() the resource no longer exists for any
method. further processing.
Note: However, calling Dispose does
not remove the connection from the
connection pool.
Example:
try
{
string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon = new SqlConnection(constring);
sqlcon.Open(); // here connection is open
// some code here which will be execute
}
catch
{
// code will be execute when error occurred in try block
}
finally
{
sqlcon.Close(); // close the connection
sqlcon.Dispose(); // desroy the connection object
}
Note: Only call the Close method on a Stream or a database connection if the object will be reused.
Otherwise, use the Dispose method.
And, further updates on difference between questions and answers, please visit my blog @
http://onlydifferencefaqs.blogspot.in/