.NET CF 2.0: Stream implements IDisposable ... sort of?
I ran into something strange in a .NET CF 2.0 for Pocket PC 2003 (Visual Studio 2005) project. I dealt with the object System.IO.Stream
and found that the IDE would not auto-populate the method Dispose()
. I typed it in manually and got:
'System.IO.Stream.Dispose (bool)' not available due to protection level
The error belongs to the protected method Dispose(bool)
. Dispose()
is personal or not.
Question 1: How is this possible? Stream
implements IDisposable
:
public abstract class Stream : MarshalByRefObject, IDisposable
... and IDisposable
a method is required Dispose()
:
public interface IDisposable
{
void Dispose();
}
I know the compiler won't let me get away with my code.
Question 2: Can I cause problems by running and locating my streams directly?
IDisposable idisp = someStream;
idisp.Dispose();
Implicit selection is accepted by the compiler.
Edit: This was already mentioned in question 939124 . The class Stream
explicitly implements IDisposable
. This is a language feature that I completely forgot about.
a source to share
Stream implements the IDisposable interface, but hides the "official" Dispose name and provides a Close method that invokes it internally. Therefore, calling Stream.Close () is equal to calling IDisposable.Dispose ().
And q2: No, it won't cause a problem, but it doesn't have to.
a source to share