In 99% of cases, the Microsoft environment is significantly complex, and the correct approach is simple:
- If your class has type fields that implement IDisposable, and no one is going to use these objects as soon as you finish with them, you should implement IDisposable, and your delete method should call Dispose in all such fields.
- If your class does not have such fields, but you think that classes that come from yours can or if your class needs to implement an interface such as IEnumerator (of T) that requires IDisposable, you should have an override Dispose method that does nothing doesn't do
- The intrinsic semantics of the "dispose" method is intended to ensure that the object does everything that is necessary to clear other objects before it fails. If an object does not need to clean up other objects, Dispose must do nothing harmlessly. There was never a reason to throw a NotImplementedException or a NotSupportedException from Dispose.
The key point in implementing IDisposable is not to clear any particular โresourceโ, but rather to ensure that if an object changes other objects in the system in such a way as to be cleared someday, these objects will be cleared while information and momentum necessary for this still exist. Ideally, this cleaning should occur as soon as possible, but not earlier. If the object contains, for example, nothing but many arrays of strings, there will be no need for cleaning. The string does not require any cleaning; an array of objects that do not require cleaning does not require any cleaning, and an object containing nothing but other objects that do not require cleaning also does not need to be cleaned. On the other hand, an action similar to opening a TCP socket creates the need to provide a specific cleanup action (closing a socket). If an object opens a TCP socket and stores information about it, the purpose of the Dispose call on this object will not destroy the socket information (which the garbage collector will take care of), but to ensure that the necessary operation is performed โcloseโ on the TCP stack.
source share