C#--- IEnumerable<T>和yield return的使用

代码分析:
(1).注意第17行,MyColl<T>的构造函数不能写成MyColl<T>...
(2).24的IEnumerable<T> GetEnumerator()来自IEnumberable<T>,  而29行的IEnumerable.GetEnumerator()来自IEnumberable。
    在泛型时候,调用第24行。但是为什么要写29行呢?因为IEnumberable<T>本身继承自  IEnumberable,即    
    public interface IEnumerable<T> : IEnumerable
    {
        IEnumerator<T> GetEnumerator();
    }
    public interface IEnumerable
    {
        IEnumerator GetEnumerator();
    }
(3).第26行的NestEnum是新建的一个类,继承自IEnumerator<T>。在类NestEnum中。
     
    T Current{get;}继承自IEnumerator<T>
    object Current { get; }   bool MoveNext();    void Reset();继承自IEnumerator
    void Dispose()继承自IDisposable
    如下

    public interface IEnumerator<T> : IDisposable, IEnumerator

(4).调用顺序是:一旦获得枚举器,它首先调用MoveNext,然后使用Current的返回值初始化变量n,如此开始循环。如果循环没有其他的出口路径,循环将持续下去直到MoveNext返回false。这是枚举器结束遍历,然后调用Dispose函数。reset一直没有被调用,我猜想可能是应该在类内部处理特殊情况的


[csharp] view plaincopy

    using System;  
    using System.Collections;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.IO;  
    using System.Windows.Forms;  
    using System.Xml;  
    using System.Reflection;  
    using System.Threading;  
    using System.Drawing;  
    using System.Collections.Specialized;  
    namespace CSharpConsole  
    {  
        public class MyColl<T>:IEnumerable<T>  
        {  
            public List<T> arr = new List<T>();  
            public MyColl(T t1,T t2,T t3,T t4)  
            {  
                arr.Add(t1);  
                arr.Add(t2);  
                arr.Add(t3);  
                arr.Add(t4);  
            }  
            public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>  
            {  
                return new NestEnum<T>(this);  
            }  
      
            IEnumerator IEnumerable.GetEnumerator()//这个函数来自IEnumerable  
            {  
      
                return GetEnumerator();  
            }  
        }  
        public class NestEnum<T> : IEnumerator<T>  
        {  
            public NestEnum(MyColl<T> coll)  
            {  
                this.coll = coll;  
                this.index = -1;  
                Console.WriteLine("CON");  
            }  
            public T Current//这个函数来自IEnumerator<T>  
            {  
                get  
                {  
                    Console.WriteLine("CURRENT T");  
                    return current;  
                }  
            }  
            public void Dispose()//这个函数来自IDisposable  
            {  
                Console.WriteLine("DISPOSE");  
             }  
            object IEnumerator.Current//这个函数来自IEnumerator  
            {  
                get  
                {  
                    Console.WriteLine("CURENT IENUMBLE");  
                    return Current;   
                }  
            }  
            public bool MoveNext()//这个函数来自IEnumerator  
            {  
                Console.WriteLine("MOVE");  
                index++;  
                if (index == coll.arr.Count)  
                    return false;  
                else  
                {  
                    current = coll.arr[index];  
                    return true;  
                }  
            }  
            public void Reset()//这个函数来自IEnumerator  
            {  
                Console.WriteLine("RESET");  
                current = default(T);  
                index = 0;  
            }  
            private MyColl<T> coll;  
            private int index;  
            private T current;  
        }  
        public class Program  
        {  
            public static void Fun()  
            {  
                MyColl<int> col = new MyColl<int>(1, 2, 3, 4);  
                foreach (int i in col)  
                {  
                }  
            }  
            static void Main()  
            {  
                Fun();  
                Console.WriteLine("-----------------------------------------");  
                MyColl<int> col=new MyColl<int>(1,2,3,4);  
                foreach (int i in col)  
                {  
      
                }  
                Console.WriteLine("-----------------------------------------");  
                foreach (int i in col)  
                {  
      
                }  
            }  
        }  
    }  

[csharp] view plaincopy

    </pre><pre name="code" class="csharp">  

[csharp] view plaincopy

    </pre><pre name="code" class="csharp">  

[csharp] view plaincopy

    </pre><pre name="code" class="csharp">  

如果用yield return的话,就变的简单多了,只要修改一个函数如下;而且NestEnum的类都不需要了
[csharp] view plaincopy

            public IEnumerator<T> GetEnumerator()//这个函数来自IEnumerable<T>  
            {  
                for (int i = 2; i < arr.Count; i++)  
                    yield return arr[i];  
                for (int i = 0; i < 2; i++)  
                    yield return arr[i];  
            }  

[csharp] view plaincopy

    </pre><p>yield return还有一种不放在类里的用法,很奇怪,注意函数返回值只能是IEnumerable<T>或者是IEnumerable 不能使IEnumerator<T>和IEnumerator,因为需要返回值里面有</p><p>GetEnumerator()方法</p><p><pre name="code" class="csharp">    public class Program  
        {  
            static void Main()  
            {  
                List<int> intlist = new List<int>();  
                intlist.Add(1);  
                intlist.Add(2);  
                intlist.Add(3);  
                intlist.Add(4);  
      
                foreach(int n in CreateReverseIterator(intlist))  
                {  
                    Console.WriteLine(n);  
                }  
            }  
            static IEnumerable<T> CreateReverseIterator<T>(IList<T> list)  
            {  
                int count = list.Count;  
                for (int i = count - 1; i >= 0; i--)  
                {  
                    yield return list[i];  
                }  
            }  
        } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值