//封装的方法 //读取文件的值,放入集合中 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(); }