I used to dispose elements of type :
SqlConnection
SqlDataAdapter
DataSet etc.
But i am not sure whether it is needed to dispose for objects of type:
string[] str = ;
ArrayList etc.
Object of some class
If it is needed..Please tell me if it is ok just to dispose it this way
If(str != null)
{
str = null;
}
Please suggest me if any better way is there to address this.
No, only objects implementing the
IDisposableinterface need to be deterministacally disposed of. Ideally you should wrap them inusing statementand not call the.Disposemethod manually:or streams:
or files:
or readers:
The
IDisposablepattern is used in .NET when an object is holding references to unmanaged objects that need to be disposed and the CLRT cannot track and dispose those unmanaged resources automatically. In this case it is up to the developer to dispose them deterministically as soon as he has finished using them (by wrapping them inusing statementsas shown previously). For all other objects that do not implement the IDisposable interface you should leave the memory menagement to the CLR and the Grbage Collector which will track them and automatically release the memory they are holding when no more references are pointing to them. You, as a developer should not worry about it.So here’s the rule of thumb: