I have two code samples and I want to know what is the deifference between them and which is better to use as best practice and performance wise:
using (TestForm test = new TestForm())
{
test.ShowDialog();
}
and the other is:
TestForm test = null;
try
{
test = new TestForm();
test.ShowDialog();
}
catch(Exception ex)
{
}
finally
{
test = null;
}
The IDisposable interface defines the Dispose method, as well as the possibility to use the “using” syntax. The dispose method of a class can be implemented to release resources, close database connections and any sort of finalizing and cleanup. Just setting the class instance to null won’t execute any of the code defined in the dispose method. As a generic rule, if a class implements IDisposable, dispose should be called when you’re finished with the class instance.