How To Destroy Managed Code And Unmanaged Code In .NET [Resolved]

Posted by Kasani007 under C# on 1/9/2017 | Points: 10 | Views : 195 | Status : [Member] | Replies : 1
How To Destroy Managed Code And Unmanaged Code In .NET ?




Responses

Posted by: Sheonarayan on: 1/9/2017 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
You do not need to destroy managed code as Garbage collector takes care of it. Ideally any class that inherits IDisposable interface should be called with using block like

using (SqlConnection conn = new SqlConnection())
{

}


OR with try, catch and finally

SqlConnection conn = new SqlConnection()
try
{
//.....
conn.Open();
// do your work
// conn.Close();
}
catch(Exception ex)
{

}
finally
{
conn.Dispose();
}
Alternatively you can call GC.Collect() function.

To destroy unmanaged code, either call their Dispose or Close object or set their variable value to null.

Thanks, hope this helps.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response