c#之IEnumerator和IEnumerable

1.创建实体Student

public class Student : IEnumerable<Student>
    {
        public static List<Student> stus = new List<Student>();

        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime CreateTime { get; set; }


        public IEnumerable<Student> this[int index]
        {
            get
            {
                yield return stus[index];
            }
        }

        public void AddStudent(Student stu)
        {
            stus.Add(stu);
        }

        public IEnumerator<Student> GetEnumerator()
        {
            foreach (var item in stus)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return stus.GetEnumerator();
        }
    }

调用:

 Student stu = new Student();
                stu.AddStudent(new Student { Age = 25, Id = 1, Name = "czj1" });
                stu.AddStudent(new Student { Age = 25, Id = 2, Name = "czj2" });
                stu.AddStudent(new Student { Age = 25, Id = 3, Name = "czj3" });

                var stu0 = stu[0];
                foreach (var item in stu0)
                {
                    Console.WriteLine($"Id:{item.Id},Name:{item.Name}");
                }
                //遍历Student类中的集合
                foreach (Student item in stu)
                {
                    Console.WriteLine($"Id:{item.Id},Name:{item.Name}");
                }

输出==》 

 

2.直接在方法中使用

public static IEnumerable<Student> GetList()
        {
            List<Student> stus = new List<Student>();
            stus.Add(new Student { Age = 25, Id = 1, Name = "czj1" });
            stus.Add(new Student { Age = 25, Id = 2, Name = "czj2" });
            stus.Add(new Student { Age = 25, Id = 3, Name = "czj3" });
            foreach (var item in stus)
            {
                yield return item;
            }
        }

调用:

static void Main(string[] args)
{
            foreach (var item in GetList())
            {
                Console.WriteLine($"Id:{item.Id},Name:{item.Name}");
            }
}

输出==》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`IEnumerator` 和 `IEnumerable` 两个接口是 C# 中用于实现迭代器的关键接口。 `IEnumerable` 接口有一个方法 `GetEnumerator()`,它返回一个实现了 `IEnumerator` 接口的对象,该对象可以迭代集合中的元素。实现了 `IEnumerable` 接口的类可以使用 `foreach` 循环迭代集合中的元素。 `IEnumerator` 接口定义了三个方法: - `MoveNext()`:将枚举数推进到集合的下一个元素。 - `Reset()`:将枚举数重置为其初始位置,即在集合中第一个元素之前。 - `Current`:获取集合中的当前元素。 `IEnumerator` 接口的实现类需要在 `MoveNext()` 方法中实现迭代器的逻辑,并在 `Current` 属性中返回当前元素的值。 示例代码: ```csharp public class MyCollection : IEnumerable { private int[] array = { 1, 2, 3, 4 }; public IEnumerator GetEnumerator() { return new MyEnumerator(array); } private class MyEnumerator : IEnumerator { private int[] array; private int position = -1; public MyEnumerator(int[] array) { this.array = array; } public bool MoveNext() { position++; return (position < array.Length); } public void Reset() { position = -1; } public object Current { get { try { return array[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } } ``` 上述代码中,`MyCollection` 类实现了 `IEnumerable` 接口,并在 `GetEnumerator()` 方法中返回了一个实现了 `IEnumerator` 接口的对象。`MyEnumerator` 类实现了 `IEnumerator` 接口,并在 `MoveNext()` 方法中实现了迭代器的逻辑,同时在 `Current` 属性中返回当前元素的值。这样,我们就可以通过 `foreach` 循环来迭代 `MyCollection` 类中的元素了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值