61

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");
nathan_jr
  • 9,092
  • 3
  • 40
  • 55
sp_m
  • 2,647
  • 8
  • 38
  • 62
  • 3
    ReadAllLines() gives you a giant array back. ReadLines() is an iterator, it helps you to avoid calling ToArray() and use foreach instead. And thus write much leaner code that uses a *lot* less memory. – Hans Passant Feb 23 '14 at 15:02
  • @sachin i am aware of Readalltext() and diffrence but was confused between readline and readallline() – sp_m Feb 23 '14 at 15:05
  • 1
    Your question is answered in [What's the fastest way to read a text file line-by-line?](http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line). – CodeCaster Feb 23 '14 at 15:06
  • 2
    Look at this: http://msdn.microsoft.com/en-us/library/dd383503(v=vs.110).aspx – fzzle Feb 23 '14 at 15:02

3 Answers3

83

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     

}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 5
    Does File.ReadLines closes the file after use in the foreach loop? – Maxter Feb 08 '19 at 16:33
  • I'd be curious to know if the read operation only continues when the enumerator advances, or if the performance difference is more of an asynchronous nature. Or perhaps more clearly: is the next line read from the file when the loop continues, or is the file read (and buffered?) asynchronously, letting us "start enumerating the collection of strings before [all lines are read]". – Softerware Jul 28 '22 at 18:10
6

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

Jacob
  • 751
  • 1
  • 6
  • 18
2

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

Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • this is incorrect. `ReadLine` reads line by line. When you use `ReadLines`, you can start enumerating the collection of strings before the whole collection is returned. From: https://msdn.microsoft.com/en-us/library/dd383503(v=vs.110).aspx – Madivad Jun 14 '18 at 13:30
  • 1
    @Madivad thanks for the comment, it was an outdated answer, I have updated it now. – Zaheer Ahmed Jun 18 '18 at 09:45