5

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

5 Answers 5

10

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

Sign up to request clarification or add additional context in comments.

3 Comments

I don't see any Dispose method there either.
Scroll down to "Explicit Interface Implementations"
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?
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.

3 Comments

+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.
What's the reasoning behind using an explicit implementation?
@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.
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

Comments

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.