views:

125

answers:

5

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

+9  A: 

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

blu
I don't see any Dispose method there either.
MCS
Scroll down to "Explicit Interface Implementations"
blu
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
See Anthony Pegram's answer
Adam
+2  A: 

It's implemented in the base class WebResponse

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

alt text

Edward Wilde
+5  A: 

It does have the Dispose method through inheritence, but it is an explicit implentation. 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
+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
What's the reasoning behind using an explicit implementation?
MCS
@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
+1  A: 

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

Paolo
+2  A: 

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