1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             mytest mt = new mytest();
13             foreach (string mychar in mt.myReverse)//注意,迭代器并不关心对象所在的类是否实现指定的方法,这里直接运用 【元素 in 对象.实现迭代的方法】 来实现遍历
14             {
15                 Console.WriteLine(mychar);
16             }
17         }
18     }
19 
20     public class mytest
21     {
22         string[] m_array = {"a","b","c" };//声明一个string数组
23         public IEnumerable<string> myReverse//这里声明一个IEnumerable<string>接口的方法,方法的名称为自定义
24         {
25             get
26             {
27                 for (int i = m_array.Length - 1; i >= 0; i--)//倒叙遍历数组
28                     yield return m_array[i];//以yield return来将遍历的每个数组元素返回给迭代器
29             }
30         }
31     }
32 }
View Code

相关文章:

  • 2023-02-22
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
  • 2021-08-26
猜你喜欢
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-19
  • 2021-08-07
  • 2021-12-23
相关资源
相似解决方案