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

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

1.迭代文件的行
復(fù)制代碼 代碼如下:
        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.使用迭代器和謂詞對文件中的行進(jìn)行篩選
復(fù)制代碼 代碼如下:
       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技術(shù)使用迭代器 遍歷文件信息的詳解,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 武邑县| 高邮市| 西藏| 吕梁市| 怀宁县| 福安市| 永兴县| 雷州市| 蒲城县| 得荣县| 广汉市| 阜城县| 克什克腾旗| 自治县| 衢州市| 卢湾区| 綦江县| 泾源县| 麻阳| 南投县| 高尔夫| 汽车| 红安县| 上思县| 香格里拉县| 祁连县| 陵川县| 宁远县| 秀山| 永仁县| 龙游县| 德阳市| 白山市| 峡江县| 布拖县| 鹤峰县| 阿鲁科尔沁旗| 丘北县| 根河市| 米泉市| 临夏县|