C #: What should be the contents of the Dispose method when implementing the IDisposable interface

I created a class that implements the IDisposable interface, and the VisualStudio IDE brings the Dispose method to me. I am wondering what code I should write inside the Dispose method so that it takes care of my memory management or what it should do.

public class ESNVerification : IDisposable { public bool Save(string a,string b) { //Save the data to table return true; } public void Dispose() { throw new NotImplementedException(); // Really what i should do here ? } } 
+4
source share
9 answers

I recommend Three simple rules on how to implement IDisposable; simplified here:

  • Rule 1: Do not do this (unless you need to). There are only two situations where IDisposable needs to be implemented: a class owns an unmanaged resource, or a class owns managed ( IDisposable ) resources.
  • Rule 2: for a class that owns managed resources, run IDisposable (but not finalizer). This implementation of IDisposable should only call Dispose for each owned resource. A class must not have a finalizer.
  • Rule 3: For a class that owns a single unmanaged resource, execute both IDisposable and the finalizer.
+3
source

If you are not working with some unmanaged resource that needs to be cleaned up (which you do not have in this extremely simple case), then you really have no reason to implement IDisposable .

First line in MSDN IDisposable interface description:

The main use of this interface to free unmanaged resources

If you used unmanaged resources, then the Dispose method ensures that these resources are correctly released (the garbage collector will take care of all your managed resources for you).

+2
source

Dispose is designed to free unmanaged resources (i.e. resources that the garbage collector does not automatically take care of after use). Common examples are database and file connections. Implementing the Dispose method provides more explanation and sample code.

This page has an important warning that the Dispose method must be implemented so that multiple calls do not throw an exception. This is because it is used by the garbage collector, and it can call the delete method several times.

+1
source
+1
source

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.

+1
source

There are two pretty decent articles on MSDN: Implementing the Dispose Method and Implementing Finalize and Dispose to Clean Up Unmanaged Resources . Seeing the first one say:

There are no performance advantages for types that use only managed resources (such as arrays) in the Dispose method, since they are automatically returned by the garbage collector.

I would recommend that you read both, decide whether to implement this, and use the code examples there as a start.

0
source

The dispose method exists so that you can free up all the resources you have allocated that are not simple objects (which are cleared by garbage collection). for instance

  • Streams
  • database connections and / or result sets
  • pointers to unmanaged objects
  • any object that you select inside your object that also implements IDisposable

should be cleared in the Dispose method (possibly by calling Dispose on these objects).

If you do not have such resources, you probably will not need to implement IDisposable. However, you can implement another interface that inherits from IDisposable, such as IEnumerator. In this case, you can leave the Dispose method empty if you do not have any of the resources mentioned above.

0
source

There are only two reasons for implementing IDisposable 1, and what you do inside the Dispose method depends on which one applies to your situation.

If your class creates instances of an object that implement IDisposable and cannot get rid of them in the method that creates them, this class will have member variables that reference these instances. This class must also implement IDisposable , and inside it you must call Dispose for each one-time item.

If you use unmanaged resources directly, your class must implement IDisposable and in your Dispose method you should handle them as needed. What exactly this means will change a lot since there is no standard interface. This is very unusual - usually you use managed classes that process them for you, such as FileStream or SqlConnection . (In this case, see above.)

1 If you intentionally use IDisposable non-standard way . (Although the merits of this are controversial .)

0
source

The IDisposable template should be used when your class uses unmanaged system resources, like pens on files, pointers ... To free the memory that they use.

If your class does not use unmanaged resources, you probably do not need to use this template.

If you really need IDisposable:

 public void Dispose() { //Free here your resources this.Dispose(true); GC.SuppressFinalize(this); } 
-1
source

Source: https://habr.com/ru/post/1340318/


All Articles