MCS
MCS

Reputation: 22541

How does this class implement IDisposable if it doesn't have a Dispose method?

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

Upvotes: 5

Views: 460

Answers (5)

Anthony Pegram
Anthony Pegram

Reputation: 126794

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.

Upvotes: 9

decyclone
decyclone

Reputation: 30810

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

Upvotes: 4

Paolo
Paolo

Reputation: 22638

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

Upvotes: 2

Edward Wilde
Edward Wilde

Reputation: 26507

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

Upvotes: 3

blu
blu

Reputation: 13175

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

Upvotes: 10

Related Questions