c# - OpenText vs ReadLines -
i came across implementation of reading file line line looks this:
using (streamreader reader = file.opentext(path)) while (!reader.endofstream) { string line = reader.readline(); }
however this:
foreach (string line in file.readlines(path)) { }
is there reason pick 1 on other?
objectively:
the first 1 picking line 1 one , process stream data.
the second 1 generates
ienumerable<string>
@ once , can start processing lines (source msdn - apologize mixreadalllines
@ first).
usefulness?:
- the first 1 more "fluid" in sense. because can choose take/process , break/continue @ will. everytime need line, take one, process , choose break or continue. can choose not take further line (suppose have
yield
inwhile
loop) till want come @ will - the second 1
ienumerable<string>
(that is, specify info of expected data before process) giving overhead @ first. noted, however, overhead relatively smallienumerable
defer actual execution, unlikelist
. good info read.
and msdn following use cases readlines
:
perform linq objects queries on file obtain filtered set of lines.
write returned collection of lines file file.writealllines(string, ienumerable<string>) method, or append them existing file the
file.appendalllines(string, ienumerable<string>) method. create populated instance of collection takes ienumerable<t> collection of strings constructor, such ilist<t> or queue<t>.
Comments
Post a Comment