3

After debugging through some code recently involving WebResponse, I found that the issue I had was that I was not properly disposing of the WebResponse before issuing another one. I was lead astray since WebResponse needs to be cast as an IDisposable in order to actually call dispose (or you can use "using" to achieve the same goal).

So my questions are:

1) What is Microsoft using to accomplish this?

IDisposable is an interface and therefore public, yet somehow WebResponse alters the access modifier to be protected according to the MSDN doumentation. I thought this was impossible.

2) What is the benefit of hiding the dispose in this manner?

Why not just let webResponse.Dispose() be valid?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
Seth Micalizzi
  • 449
  • 6
  • 17
  • How are you properly disposing of `WebResponse` now? According to [the documentation](http://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx), WebResponse not only inherits from `IDisposable`, it also implements the `Dispose()` method. All you should need to do is wrap your `WebResponse` object in a `using` block. – Robert Harvey Oct 21 '13 at 19:42
  • Why don't you just use "using"? It automatically disposes of the object anyway. http://msdn.microsoft.com/en-us/library/yh598w02.aspx – Jon La Marr Oct 21 '13 at 19:43
  • I do use "using" (I mentioned it in the question). Just curious why this was the case. – Seth Micalizzi Oct 21 '13 at 19:46
  • @RobertHarvey now hit the "Other Versions" drop down; I'm guess that the OP is using <= .NET 3.5 – Marc Gravell Oct 21 '13 at 19:47
  • @RobertHarvey No, I am using 4.0. Just thought this was a good thing to use as a learning experience. – Seth Micalizzi Oct 21 '13 at 19:51
  • @SethMicalizzi that method is public in 4.0 – Marc Gravell Oct 21 '13 at 19:54
  • @MarcGravell Am I looking in the incorrect place? http://imgur.com/C6EnntA (I don't intend that to be sarcastic, hard to convey that over the internet. :-/) – Seth Micalizzi Oct 21 '13 at 20:02
  • @Seth I've checked, and it looks like it is there in 4.5, but not 4.0 - oh nos, a MISTAKE IN MSDN!!! – Marc Gravell Oct 21 '13 at 20:09
  • @MarcGravell Thats it, time to switch to Ruby where things are perfect all the time ;) Thanks for the help Marc. – Seth Micalizzi Oct 21 '13 at 20:13

1 Answers1

6

Explicit interface implementation:

public class Foo : IDisposable {
    void IDisposable.Dispose() { /* code here */ }
}

This can be done with any interface method. The using API knows to use the IDisposable implementation.

Note that this feature should not be over-used; the following would be confusing, for example:

public class Foo : IDisposable {
    void IDisposable.Dispose() { /* do something */ }
    public void Dispose() { /* do something completely different */ }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    @RobertHarvey before .NET 4.0, yes – Marc Gravell Oct 21 '13 at 19:46
  • Shouldn't this still show up in the documentation when I go to definition on WebResponse? – Seth Micalizzi Oct 21 '13 at 19:47
  • @SethMicalizzi: Not if it's `private`. – Robert Harvey Oct 21 '13 at 19:47
  • I may be asking you to peer into the heads of the team at Microsoft, but why use Explicit interface implementation in this case? What is the benefit of hiding it? – Seth Micalizzi Oct 21 '13 at 19:49
  • 1
    @Seth to be fair, the only benefit in *not* hiding it is making it obvious that it exists in the first place - it is rare that you would want to call it yourself. However, since it now **is** public, they seem to agree with you: I'm not sure that it is sensible to speculate on that historically – Marc Gravell Oct 21 '13 at 19:51
  • @MarcGravell Fair enough. Microsoft engineers rarely make such decisions lightly, so I figured that this might be a good model that I can apply in other situations in the future. But since it seems to be a temporary hiccup we can only really speculate. I would argue that not hiding it is more in line with the purpose of interfaces and can't really see why you would want to change the access modifier unless you are using interfaces incorrectly. – Seth Micalizzi Oct 21 '13 at 19:56