I am currently using a code construction like this:
string line;
using (System.IO.StreamReader file = new System.IO.StreamReader("somelargefile.txt"))
{
while ((line = file.ReadLine()) != null)
// do something
}
I use this to read in a very large text file. (I need to evaluate line by line, and some lines need to be stored in memory). I find it strange that this piece of code does not cause performance penalties. I assumed that every ReadLine call would result in an access to the disk making my program extremely slow on files that are several hundreds of megabytes large. But it appears to be quite fast actually. Why is this?
Is there no relationship between ReadLine and physical disk access then?
StreamReader, and I believe its parent classTextReader, use a buffered read. The system reads a block of raw data first. You can use theDiscardBuffer()method ofStreamReaderto empty this buffer manually, but I am not aware of a way to access it.MSDN has a detailed explanation of the principles.