Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 658
iTayb Avatar asked Jun 03 '10 11:06

iTayb


People also ask

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.

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

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.

like image 93
LukeH Avatar answered Sep 18 '22 19:09

LukeH


File.ReadAllText() returns one big string containing all the content of the file while File.ReadAllLines() returns string array of lines in the file.

Keep in mind that in case of ReadAllText "The resulting string does not contain the terminating carriage return and/or line feed."

More details are available at remarks section of File.ReadAllText Method and File.ReadAllLines Method.

like image 44
Giorgi Avatar answered Sep 19 '22 19:09

Giorgi