FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. How is that possible?
views:
125answers:
5
+9
A:
Its implemented in the base class WebResponse, see http://msdn.microsoft.com/en-us/library/system.net.webresponse_methods.aspx
blu
2010-06-25 14:38:26
I don't see any Dispose method there either.
MCS
2010-06-25 14:39:14
Scroll down to "Explicit Interface Implementations"
blu
2010-06-25 14:39: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
2010-06-25 14:41:26
See Anthony Pegram's answer
Adam
2010-06-25 14:43:15
+2
A:
It's implemented in the base class WebResponse
void IDisposable.Dispose()
{
try
{
this.Close();
this.OnDispose();
}
catch
{
}
}
Edward Wilde
2010-06-25 14:39:32
+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
2010-06-25 14:40:08
+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
2010-06-25 14:42:41
@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
2010-06-25 14:48:25
+1
A:
It inherits from System.Net.WebResponse which implements these methods.
Paolo
2010-06-25 14:40:12
+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
2010-06-25 14:42:52