According to the explanation by Microsoft for The ReadLines
and ReadAllLines
methods, When you use ReadLines
, you can start enumerating the collection of strings before the whole collection is returned. When you use ReadAllLines
, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines
can be more efficient.
What does it actually mean when they say:
1 - "When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned."
If the below line of code is written, then doesn't it mean that ReadLines
method execution is over and that the whole collection is returned & stored in variable filedata?
IEnumerable<String> filedata = File.ReadLines(fileWithPath)
2 - "When you use ReadAllLines
, you must wait for the whole array of strings be returned before you can access the array". Does it mean that, in the below code snippet if a large file is read then the array variable hugeFileData will not have all the data if used immediately after the file was read?
string[] hugeFileData = File.ReadAllLines(path)
string i = hugeFileData[hugeFileData.length-1];
3 - "when you are working with very large files, ReadLines can be more efficient". If that is so, is the below code efficient when reading large file? I believe that the 2nd and 3rd line the below code would read the file twice, correct me if I am wrong.
string fileWithPath = "some large sized file path";
string lastLine = File.ReadLines(fileWithPath).Last();
int totalLines = File.ReadLines(fileWithPath).Count();
The reason of calling ReadLines on the same file twice in the above code snippet is that when I tried the below code, I got an exception "Cannot read from a closed TextReader
" on the 3rd line in the below code snippet.
IEnumerable<String> filedata = File.ReadLines(fileWithPath);
string lastLine = filedata.Last();
int totalLines = filedata.Count();