没有合适的资源?快使用搜索试试~ 我知道了~
使用迭代器 遍历文件信息的详解
需积分: 3 0 下载量 185 浏览量
2020-12-18
19:08:54
上传
评论
收藏 25KB PDF 举报
温馨提示
试读
1页
1.迭代文件的行复制代码 代码如下: public static IEnumerable<string> ReadLines(string fileName) { using (TextReader reader = File.OpenText(fileName)) { string line; if ((line = reader.ReadLine()) != null) { yield ret
资源推荐
资源详情
资源评论
使用迭代器使用迭代器 遍历文件信息的详解遍历文件信息的详解
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);
}
}
您可能感兴趣的文章您可能感兴趣的文章:PHP迭代器的内部执行过程详解PHP迭代器实现斐波纳契数列的函数PHP设计模式之迭代器模式的深入
解析ruby 迭代器使用方法python迭代器的使用方法实例
资源评论
weixin_38713801
- 粉丝: 5
- 资源: 930
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功