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 mix readalllines @ 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 in while 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 small ienumerable defer actual execution, unlike list. 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

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -