What is the difference between File.ReadLines() and File.ReadAllLines()?

C#Readlinefile.readalllines

C# Problem Overview


I have query regarding File.ReadLines() and File.ReadAllLines().what is difference between them. i have text file where it contains data in row-wise.File.ReadAllLines() return array and using File.ReadLines().ToArray(); will also i can get same result.So is there any performance difference related to these methods?

string[] lines = File.ReadLines("C:\\mytxt.txt").ToArray();

Or

string[] lines = File.ReadAllLines("C:\\mytxt.txt");

C# Solutions


Solution 1 - C#

> is there any performance difference related to these methods?

YES there is a difference

File.ReadAllLines() method reads the whole file at a time and returns the string[] array, so it takes time while working with large size of files and not recommended as user has to wait untill the whole array is returned.

File.ReadLines() returns an IEnumerable<string> and it does not read the whole file at one go, so it is really a better option when working with large size files.

From MSDN:

> The ReadLines and ReadAllLines methods differ as follows:

> 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.

Example 1: File.ReadAllLines()

string[] lines = File.ReadAllLines("C:\\mytxt.txt");

Example 2: File.ReadLines()

foreach (var line in File.ReadLines("C:\\mytxt.txt"))
{

   //Do something     

}

Solution 2 - C#

File.ReadLines Method returns IEnumerable<string> . But File.ReadAllLines returns string[] If you read a Big file you better use File.ReadLines Method. becouse its reads line after line from the file, not read the all file into string[] which take a lot of memory. MSDN

Solution 3 - C#

ReadAllLines

>Opens a text file, reads all lines of the file, and then closes the file.

If you have small files or have to process the complete file at once then use this as it completely read the file which means the whole file is in your memory In case of large files it may slow down the performance.

ReadLines

>With the File.ReadLines method, you can start enumerating the collection of strings before the whole collection is returned.

It is useful if you need to process the file in chunks(not the whole file at once).

Remarks stated in MSDN:

>The ReadLines and ReadAllLines methods differ as follows: > >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.

Here is more details and comparison demo available

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsp_mView Question on Stackoverflow
Solution 1 - C#Sudhakar TillapudiView Answer on Stackoverflow
Solution 2 - C#JacobView Answer on Stackoverflow
Solution 3 - C#Zaheer AhmedView Answer on Stackoverflow