42

Example:

variable = new StreamReader( file ).ReadToEnd();

Is that acceptable?

Matt Ralston
  • 666
  • 1
  • 6
  • 15

6 Answers6

51

No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it's GC'd sooner):

using (StreamReader r = new StreamReader("file.txt"))
{
  allFileText = r.ReadToEnd();
}

Or alternatively in .Net 2 you can use the new File. static members, then you don't need to close anything:

variable = File.ReadAllText("file.txt");
badbod99
  • 7,429
  • 2
  • 32
  • 31
  • 2
    `ReadAllText` was added in .Net 2.0 (http://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=VS.80).aspx) – JaredPar Nov 09 '10 at 18:00
  • Thanks Jared (edited answer). TBH I only came across it recently. Once you know one way you generally tend to stick with what works. – badbod99 Nov 10 '10 at 10:10
  • 1
    Is there a perfomance difference when using StreamReader.ReadToEnd vs File.ReadAllTest? – Mike Jul 06 '18 at 20:24
21

You should always dispose of your resources.

// the using statement automatically disposes the streamreader because
// it implements the IDisposable interface
using( var reader = new StreamReader(file) )
{
    variable = reader.ReadToEnd();
}

Or at least calling it manually:

reader = new StreamReader(file);
variable = reader.ReadToEnd();
reader.Close();
Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • 1
    For someone who doesn't know that the using statement automatically takes care of this for you, this would be confusing. No down vote, but no up vote either because this is just not worded for a beginner to understand. – David Nov 09 '10 at 17:24
  • 1
    Added a comment to be helpful – Dismissile Nov 09 '10 at 17:25
  • 4
    In your second example you don't call *Dispose()* at all. And there is no *try-catch* mechanism to make sure that *Close()* is called. When you add both, a *try-catch* and a *Dispose()* call it will result in nothing else than a *using*, like in your first example. – tanascius Nov 09 '10 at 17:41
  • 1
    This explains it well however it doesn't mention, as badbod99 and a couple others did, "Or alternatively in .Net 4 you can use the new File. static members, then you don't need to close anything" – Matt Ralston Nov 09 '10 at 17:50
7

You need to Dispose of objects that implement IDisposable. Use a using statement to make sure it gets disposed without explicitly calling the Dispose method.

using (var reader = new StreamReader(file))
{
  variable = reader.ReadToEnd();
}

Alternately, use File.ReadAllText(String)

variable = File.ReadAllText(file);
Greg
  • 23,155
  • 11
  • 57
  • 79
  • +1 but I'd word it "you don't need to explicitly close it if you use a "using" statement to make sure it gets disposed. +1 because this is more clearly worded than Dismissile's answer. His answer does nothing to explain that the using statement will dispose of the resources for you, and someone who didn't know that would be left scratching their head. – David Nov 09 '10 at 17:23
4

Yes, whenever you create a disposable object you must dispose of it preferably with a using statement

using (var reader = new StreamReader(file)) {
  variable = reader.ReadToEnd(file);
}

In this case though you can just use the File.ReadAllText method to simplify the expression

variable = File.ReadAllText(file);
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • This was helpful though badbod99 supplied a few more specifics. I'm assuming those specifics are correct as I"m only just getting into code outside of javascript. – Matt Ralston Nov 09 '10 at 17:53
0

It's good practice to close StreamReader. Use the following template:

string contents;

using (StreamReader sr = new StreamReader(file)) 
{
   contents = sr.ReadToEnd();
}
Bernard
  • 7,908
  • 2
  • 36
  • 33
0

You're better off using the using keyword; then you don't need to explicitly close anything.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117