I have read a lot about finalizer and IDisposable in C#. As I finally become clear from this monstrous confusion over finalizer and IDisposable, suddenly, out of nowhere, there is this SafeHandle thing.
My belief is completely shaken again. What am I supposed to use?
I have read a lot about finalizer and IDisposable in C#. As I finally
Share
SafeHandle is only useful when dealing with Win32 Interop calls. In Win32, most things are represented by “handles”. This includes Windows, Mutexes, etc. So the .NET SafeHandle uses the disposable pattern to ensure the Win32 handle is properly closed.
So if you are using Win32 Interop calls and getting back Win32 handles, then use SafeHandle. For your own objects, you would stick with IDisposable and a finalizer.