色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

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

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技術使用迭代器 遍歷文件信息的詳解,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 宁津县| 韶山市| 苏尼特左旗| 安西县| 和硕县| 丹阳市| 盐城市| 鄂伦春自治旗| 永靖县| 民权县| 乐至县| 敖汉旗| 右玉县| 丹江口市| 成安县| 马尔康县| 花莲市| 界首市| 莆田市| 保靖县| 文昌市| 仙桃市| 榕江县| 电白县| 广德县| 乌拉特前旗| 寿阳县| 会泽县| 上蔡县| 疏勒县| 连江县| 宜章县| 临城县| 五原县| 自治县| 大理市| 通榆县| 海兴县| 财经| 洛隆县| 临夏县|