Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"); 
like image 892
sp_m Avatar asked Feb 23 '14 14:02

sp_m


People also ask

What is the difference between ReadAllLines and ReadAllText in C#?

ReadAllLines returns an array of strings. Each string contains a single line of the file. ReadAllText returns a single string containing all the lines of the file.

When manipulating a file What is the difference between foreach and ReadLines methods?

Explanation: The difference between the method foreach and the method readlines is that the method foreach is associated with a block. However, unlike the method readlines, the method foreach does not return an array. 9.

How does file ReadLines work?

The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned. If the total number of bytes returned exceeds the specified number, no more lines are returned.

Does ReadLine read all lines?

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.


2 Answers

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       } 
like image 122
Sudhakar Tillapudi Avatar answered Oct 08 '22 01:10

Sudhakar Tillapudi


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

like image 23
Jacob Avatar answered Oct 08 '22 01:10

Jacob