C# static方法-使用迭代器迴圈遍曆檔案中的額行

來源:互聯網
上載者:User

標籤:blog   ar   使用   for   檔案   div   on   log   ad   

//封裝的方法//讀取檔案的值,放入集合中        public static IEnumerable<string> ReadLines(string fileName) {            using (TextReader reader=File.OpenText(fileName)) {                string line;                while ((line=reader.ReadLine())!=null) {                    yield return line;                }            } }//調用 class Program {        static void Main(string[] args) {            foreach (var item in ReadLines("~/map/123.txt")) {                Console.WriteLine(item);            }            Console.ReadKey(); }//x^n public static IEnumerable<int> Power(int number, int exp) {            int result = 1;            for (int i = 0; i < exp; i++) {                result = result * number;                yield return result;            }        }//調用,結果 2,4,8,16,32 class Program {        static void Main(string[] args) {             foreach (var item in Power(2, 5)) {                Console.WriteLine(item);            }            Console.ReadKey(); }

  

C# static方法-使用迭代器迴圈遍曆檔案中的額行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.