What is the null field used for in the streamreader class?

G

garyusenet

From MSDN I'm trying to learn more about streamreader. I'm working my
way down the MSDN definition. The first entry is the Public Constructor
StreamReader. I'm fairly happy I have a basic grasp on this. It is the
part of the Streamreader class that creates a StreamReader instance for
a given Stream. The next entry however I don't understand: -

Public Fields
Name Description
Null A StreamReader object around an empty stream.

I have used go to definition and looked at this and can see that it
corresponds to the following code:

public class StreamReader : TextReader
{
// Summary:
// A System.IO.StreamReader object around an empty stream.
public static readonly StreamReader Null;

What does this do? I don't understand. Could someone please explain
it's use.

Thankyou,

Gary.
 
J

Jon Skeet [C# MVP]

way down the MSDN definition. The first entry is the Public Constructor
StreamReader. I'm fairly happy I have a basic grasp on this. It is the
part of the Streamreader class that creates a StreamReader instance for
a given Stream. The next entry however I don't understand: -

Public Fields
Name Description
Null A StreamReader object around an empty stream.

I have used go to definition and looked at this and can see that it
corresponds to the following code:

public class StreamReader : TextReader
{
// Summary:
// A System.IO.StreamReader object around an empty stream.
public static readonly StreamReader Null;

What does this do? I don't understand. Could someone please explain
it's use.

Every so often it's useful to have a way of writing to a null stream or
writer - if you're forced to provide a "sink" for logging, for example,
but you don't actually want to write anything.

Using a null *reader* would be rarer, but it's basically just a
StreamReader which never returns any data. I can't immediately think of
any uses, but I'm sure there are some...
 

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