About the abstract class TextReader and the normal StreamReader class

T

TonyJ

Hello!!

If I look in the .NET documentation for the abstract class TextReader and
method Dispose it
says TextReader.Dispose().

If I look at the Dispose method in the derived class StreamReader it says
StreamReader.Dispose().

I just wonder what does it mean when a method is prefixed with the class
name like
the TextReader.Dispose(). and StreamReader.Dispose(). for the two classes
TextReader and StreamReader.

One more question.
Is there actually an implementation for the Dispose in the StreamReader or
does the StreamReader inherit the Dispose from TextReader.

//Tony
 
M

Marc Gravell

I just wonder what does it mean when a method is prefixed
with the class name like
It is just making it clear which class it is talking about; it doesn't
say much about where (in the hierarchy) the method came from.
Is there actually an implementation for the Dispose in the
StreamReader or
does the StreamReader inherit the Dispose from TextReader.
Yes, Dispose() is inherited. It isn't "virtual" - but it calls the
"protected virtual" Dispose(bool) method - and here StreamReader adds
some code to close the stream etc as appropriate.

Either way - the easiest way to call Dispose() is via "using":

using(StreamReader reader = new StreamReader(whatever)) {
/// ...
}

The "protected virtual Dispose(bool)" pattern is quite common, as it
allows a single place to put both finalizer and IDisposable code.

Marc
 
D

DeveloperX

TonyJ said:
Hello!!

If I look in the .NET documentation for the abstract class TextReader and
method Dispose it
says TextReader.Dispose().

If I look at the Dispose method in the derived class StreamReader it says
StreamReader.Dispose().

I just wonder what does it mean when a method is prefixed with the class
name like
the TextReader.Dispose(). and StreamReader.Dispose(). for the two classes
TextReader and StreamReader.

One more question.
Is there actually an implementation for the Dispose in the StreamReader or
does the StreamReader inherit the Dispose from TextReader.

//Tony


When you have an interface you can implement it implicitly or
explicitly. The above is an explicit implementation.

I couldn't find the article I usually offer when trying to explain
this, but this one seems quite good:

http://blogs.msdn.com/mhop/archive/2006/12/12/implicit-and-explicit-interface-implementations.aspx
 
M

Marc Gravell

The above is an explicit implementation.

Sorry, but no it isn't; it is implicit; there is a public callable
method, and there is no overriding "void IDisposable.Dispose()".

* Just a public .SomeMember => implicit
* Just a non-public ISomeInterface.SomeMember => explicit
* Both a public .SomeMember and a non-public ISomeInterface.SomeMember
=> explicit

Marc
 
T

TonyJ

Hello!

I wonder how does TextReader's Dispose call the Dispose(bool) in
StreamReader.
In other word how can a Base class call a method in a derived class?

//Tony
 
M

Marc Gravell

how can a Base class call a method in a derived class?

It is declared as virtual in the base class, but overridden in the
derived class; "virtual" means a that a derived class can replace the
implementation of a method:

public abstract class Foo : IDisposable
{
~Foo() {
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{ // nothing here, but subclases can add
}
}
public class Bar : Foo
{
protected override void Dispose(bool disposing)
{
if (disposing) {
// clean up managed resources
}
// release references and
// clean up unmanaged resources

// call the base method in case it does something
// worthwhile (which it doesn't in this case)
base.Dispose(disposing);
}
}
 
D

DeveloperX

Sorry, but no it isn't; it is implicit; there is a public callable
method, and there is no overriding "void IDisposable.Dispose()".

* Just a public .SomeMember => implicit
* Just a non-public ISomeInterface.SomeMember => explicit
* Both a public .SomeMember and a non-public ISomeInterface.SomeMember
=> explicit

Marc

May I just say Doh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top