Should IDisposable.Dispose() be made safe to call multiple times?

Solution 1:

You should be safe to call it more than once, though you should probably avoid it if you can.

From the MSDN page on IDisposable.Dispose():

If an object's Dispose method is called more than once, the object must ignore all calls after the first one. The object must not throw an exception if its Dispose method is called multiple times.

Solution 2:

Yes, your implementations of IDisposable.Dispose() should tolerate being called multiple times. After the first call to Dispose(), all other calls can just return immediately.

I would prefer the first part of your code example, to dispose and null the local variables as you go.

Be aware that your .Dispose() may be called multiple times even if you implement Dispose and null patterns in your code. If multiple consumers hold a reference to the same disposable object, then that object's Dispose will probably be called multiple times as those consumers drop their references to it.

Solution 3:

Objects should be tolerant to having Dispose called more than once, since--especially in the presence of exceptions--it may be difficult for cleanup code to know for certain which things have been cleaned up and which have not. Nulling out IDisposable fields as they are cleaned up (and being tolerant of ones that are already null) will make it easier to avoid redundant calls to Dispose, but it doesn't really cost anything to make objects tolerate multiple disposal, and it helps to avoid ickiness in some Exception-throwing situations.