inheritance - C#:What should be the content of the Dispose method when implementing IDisposable interface -
i created class implements idisposable interface , visualstudio ide brought dispose method me. wondering code should write inside dispose method take care of memory management or whatever stuff should do.
public class esnverification : idisposable { public bool save(string a,string b) { //save data table return true; } public void dispose() { throw new notimplementedexception(); // should here ? } }
i recommend three simple rules how implement idisposable; simplified here:
- rule 1: don't (unless need to). there 2 situations when
idisposable
need implemented: the class owns unmanaged resource or the class owns managed (idisposable
) resources. - rule 2: class owning managed resources, implement
idisposable
(but not finalizer). implementation ofidisposable
should calldispose
each owned resource. class should not have finalizer. - rule 3: class owning single unmanaged resource, implement both
idisposable
, finalizer.
Comments
Post a Comment