Search code examples
c#dispose

Should I call Dispose method on an Image get from the Clipboard in C#


In C#/.NET, the method Clipboard.GetData() returns an object from the Clipboard.

If the object returned implements the IDisposable interface (such as an instance of the class Image), is it my responsibility to call the Dispose method on it (or use the "using" construct)?

The documentation of GetData does not says anything particular, so I assume the Clipboard object properly disposes everything. But my assumptions might be wrong.


Solution

  • Dispose method is supposed to be done if you use some unmanaged resources (file handles, unmanaged memory, etc). In such cases you should implement IDisposable and release resources in Dispose method.

    Since you use GetData which return native .NET Object, you don't have to call Dispose method.

    An example is provided here.