国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

使用迭代器 遍歷文件信息的詳解

2024-05-04 23:10:30
字體:
來源:轉載
供稿:網友
1.迭代文件的行

復制代碼 代碼如下:


public static IEnumerable<string> ReadLines(string fileName)
{
using (TextReader reader = File.OpenText(fileName))
{
string line;
if ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
static void Main()
{
foreach (string line in Iterator.ReadLines(""))
{
Console.WriteLine(line);
}
}


2.使用迭代器和謂詞對文件中的行進行篩選

復制代碼 代碼如下:


public static IEnumerable<T> where<T>(IEnumerable<T> source, Predicate<T> predicate)
{
if (source == null || predicate == null)
{
throw new ArgumentNullException();
}
return WhereImplemeter(source, predicate);
}
private static IEnumerable<T> WhereImplemeter<T>(IEnumerable<T> source, Predicate<T> predicate)
{
foreach (T item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
static void Main()
{
IEnumerable<string> lines = File.ReadAllLines(@"your file name");
Predicate<string> predicate = delegate(string line)
{
return line.StartsWith("using");
};
foreach (string str in where(lines, predicate))
{
Console.WriteLine(str);
}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 华容县| 嘉定区| 唐河县| 长白| 弥渡县| 黎城县| 西和县| 鱼台县| 巫山县| 双辽市| 屏南县| 荆州市| 禹州市| 马尔康县| 翼城县| 潢川县| 肃北| 阳西县| 临泉县| 霍邱县| 维西| 牟定县| 连城县| 云林县| 阿克陶县| 伽师县| 禹州市| 阳信县| 塘沽区| 安化县| 澄城县| 永嘉县| 四平市| 井研县| 牟定县| 鄂伦春自治旗| 招远市| 诸城市| 玉环县| 丹棱县| 郑州市|