5

FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. How is that possible?

MCS
  • 22,113
  • 20
  • 62
  • 76

5 Answers5

10

Its implemented in the base class WebResponse, see http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx

blu
  • 12,905
  • 20
  • 70
  • 106
  • I don't see any Dispose method there either. – MCS Jun 25 '10 at 14:39
  • 2
    Scroll down to "Explicit Interface Implementations" – blu Jun 25 '10 at 14:39
  • Oh, it is there, just further down the page under "Explicit Interface Implementations." Why is that? And why doesn't this method show up in IntelliSense when I have an instance of FtpWebResponse? – MCS Jun 25 '10 at 14:41
9

It does have the Dispose method through inheritance, but it is an explicit implementation. To call it, you would have to use

((IDisposable)myObject).Dispose();

Or, of course, just wrap it in a using block, as it does the explicit call for you.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • 2
    +1 for mentioning its explicitly implemented. As others have said its on the base, but you'd still see it - you answered why you can't easily see it. – Adam Houldsworth Jun 25 '10 at 14:42
  • 2
    What's the reasoning behind using an explicit implementation? – MCS Jun 25 '10 at 14:43
  • @MCS, that's a good question! Actually, it sounds like a good *new* question for stackoverflow of why some classes have explicit IDisposable implementations, as it sort of masks the fact that the object should be disposed for the average developer who is not consulting the documentation every time he/she uses a class. – Anthony Pegram Jun 25 '10 at 14:48
4

When you implement an interface explicitly, you won't get the method in the listing. You will have to cast that object to implemented interface to get access to that method.

public class MyClass : IDisposable
{
    void IDisposable.Dispose()
    {
        throw new NotImplementedException();
    }
}

Reference : http://msdn.microsoft.com/en-us/library/ms173157.aspx

decyclone
  • 30,394
  • 6
  • 63
  • 80
3

It's implemented in the base class WebResponse

void IDisposable.Dispose()
{
try
{
    this.Close();
    this.OnDispose();
}
catch
{
}
}

alt text http://img227.imageshack.us/img227/2428/redgatesnetreflector.png

Edward Wilde
  • 25,967
  • 8
  • 55
  • 64
2

It inherits from System.Net.WebResponse which implements these methods.

Paolo
  • 22,188
  • 6
  • 42
  • 49