日期:2014-05-18  浏览次数:20882 次

C#中返回接口IEnumerator
Primes类中中包含返回IEnumerator接口的GetEnumerator()的方法,但是Primes类并没有派生IEnumerator的接口,返回的IEnumerator接口是在那里实现的,返回的是什么类的实例?
代码如下:
C# code
    
public class Primes
    {
        private long min;
        private long max;

        public Primes()
            : this(2, 100)
        { }

        public Primes(long minimum, long maximum)
        {
            if (minimum < 2) min = 2;
            else min = minimum;

            max = maximum;
        }

        public IEnumerator GetEnumerator()
        {
            for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
            {
                bool isPrime = true;
                for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
                {
                    long remainderAfterDivision = possiblePrime % possibleFactor;
                    if (remainderAfterDivision == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    yield return possiblePrime;
                }

            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Primes primesFrom2To1000 = new Primes(1000000000000, 1000000010000);
            IEnumerator myEnumerator = primesFrom2To1000.GetEnumerator();
            myEnumerator.MoveNext();
            myEnumerator.MoveNext();
            //foreach (long i in primesFrom2To1000)
            //    Console.Write("{0} ", i);
            Console.ReadKey();
        }
    }



------解决方案--------------------
你的理解错误
primes 并没有实现ienum...
只是返回一个

------解决方案--------------------
yield return 是在 .Net 2.0 才有的产物。
使用 yield return 语句,编译器就会在编译的时候,生成一个实现Ienumator接口的类.并且自动维护该类的状态.比如movenext
------解决方案--------------------
关于 yield return ,给个小例子:

C# code

namespace yieldReturnPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> docs1 = new List<string>();
            docs1.Add("1");
            docs1.Add("2");
            docs1.Add("3");
            docs1.Add("4");
            docs1.Add("5");

            foreach (var item in Documents1(docs1))
            {
                Console.Write(" {0} ", item);
            }

            Console.Write("\n***********************************\n");

            foreach (var item in Documents2(docs1))
            {
                Console.Write(" {0} ", item);
            }

            Console.ReadKey();
        }

        static IEnumerable Documents1(List<string> docs)
        {
            foreach (var item in docs)
            {
                if (item != "4")
                    yield return item;
            }

            Console.Write(" {0} ", "Holle Danny");
        }

        static IEnumerable Documents2(List<string> docs)
        {
            foreach (var item in docs)
            {
                if (item != "4")
                    yield return item;
                else
                    yield break;
            }

            Console.Write(" {0} ", "Holle Danny");
        }
    }
}

------解决方案--------------------
编译器会创建一个实现了IEnumertor接口的类,并将其返回