テキストファイル(つかストリーム)を一行ずつ読み出すって
日常茶飯事のコード:
TextReader reader = new StreamReader("ふぁいる");
string line;
while ( (line = reader.ReadLine()) != null ) {
? Console.WriteLine(line);
}
reader.Close();
まいどおなじみのパターンですねぃ。
IEnumerable を返してくれるメソッドが見当たりません。
# 探し方がヘタなんでしょか...
MSDNの森を彷徨うのに疲れたのでこしらえました。
using System.IO;
using System.Collections.Generic;
namespace SilverBouquet.IO {
? public static class Util {
??? public static IEnumerable ReadLines(TextReader reader) {
????? string result;
????? while ( (result = reader.ReadLine()) != null ) {
??????? yield return result;
????? }
??? }
? }
}
// おためし
public class Program {
? public static void Main() {
??? TextReader reader = new StreamReader("ふぁいる");
??? foreach ( string line in SilverBouquet.IO.Util.ReadLines(reader) ) {
????? System.Console.WriteLine(line);
??? }
??? reader.Close();
? }
}
επιστημηはSilverBouquetプロジェクトを応援します。
使えそうならパクってくだちい > とりこびっち
# yieldってステキ♪