使用迭代器 遍历文件信息的详解
Jun 06, 2016 pm 08:31 PM本篇文章是对使用迭代器 遍历文件的信息进行了详细的分析介绍,需要的朋友参考下
1.迭代文件的行
复制代码 代码如下:
public static IEnumerable
{
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
{
if (source == null || predicate == null)
{
throw new ArgumentNullException();
}
return WhereImplemeter(source, predicate);
}
private static IEnumerable
{
foreach (T item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
static void Main()
{
IEnumerable
Predicate
{
return line.StartsWith("using");
};
foreach (string str in where(lines, predicate))
{
Console.WriteLine(str);
}
}

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

利用多光照資訊的單視角NeRF演算法S^3-NeRF,可恢復場景幾何與材質訊息
