1

The class RabbitMQ.Client.MessagePatterns.SimpleRpcServer implements IDisposable using an explicit interface implementation. The implementation is:

void IDisposable.Dispose()
{
    Close();
}

That means that in order to call Dispose() on such a object, I first have to cast it to IDisposable. Why is the Dispose method declared in this way? Should I bother to call Dispose() on a SimpleRpcServer object? I could forget it and simply call Close(), I'm just asking because FxCop gave me a warning about it and I don't think I've seen explicit interface implementations before.

user2023861
  • 8,030
  • 9
  • 57
  • 86
  • 2
    If you use it in a `using` statement you shouldn't need to cast it. Looking at the code, it was not done for naming conflict reasons. So my guess is `Dispose` is not important on this type or they are encouraging you to use `using`. – Adam Houldsworth Feb 12 '14 at 15:57

3 Answers3

1

This implementation was present in some .NET classes. The confusion related to whether you needed to call Close, Dispose, or both, when really they all perform the same action.

An example of a class that implemented it this was is RegistryKey. In .NET 3.5 and before, it explicitly implemented the IDisposable interface. This encouraged you to either use it in a using or call the Close method.

In .NET 4.x and beyond, the IDisposable interface was implicitly implemented.

I believe Stream was implemented in a similar manner.

CodeNaked
  • 40,753
  • 6
  • 122
  • 148
1

IDisposable came relatively late to the .NET Framework 1.0, as can be seen from this discusson from October 2000, and particularly the following extract:

First, we are discussing formalizing a design pattern that we already use within the framework. All of our resource classes already support a method called Dispose(). It is a public method that you can call at any time that will release the resources contained by the object. Think of it kind of like the c++ delete operator but it doesn't actually free the memory, it just runs the destructor. We are considering making this an interface to help codify the design pattern and facilitate some language support as described below.

I believe (*) this is why many .NET 1.0 classes expose a Close method and only implement IDisposable explicitly. And I think this was considered to be a mistake, which is why in later versions of .NET it is more common to see IDisposable implemented implicitly. For example, a public Dispose method was added to the Stream class in .NET 2.0.

(*) my supposition, I don't have any inside knowledge.

Joe
  • 122,218
  • 32
  • 205
  • 338
0

It could be to maintain a common verb if the framework is provided in multiple languages where Dispose might not mean anything

Or it could be done because Dispose might not make as much sense if there is also an Open method since Close is a common opposing verb to Open.

For example, the ADO DbConnection classes all do this:

connection.Open();
conncetion.Close();

Alternativly, it could be that IDisposable was implemented after the product was already shipped and had an existing Close method and to maintain backwards compatibility Dispose was hidden by explicit implementation which Close then calls:

e.g.

// version 1
public class Thing
{
    public void Close() { ... }
}

// version 2
public class Thing : IDisposable
{
    public void Close() { ... }
    void IDisposable.Dispose() { Close(); }
}
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60