using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    public class Primes : IEnumerable
    {
        private long min;
        private long max;
        public Primes()
            : this(2, 100)
        {
        }
        public Primes(long minimum, long maximum)
        {
            min = minimum;
            if (min < 2)
            {
                min = 2;
            }
            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;
                }
            }
        }
    }
}=======================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    public class Primes
    {
        private long min;
        private long max;
        public Primes()
            : this(2, 100)
        {
        }
        public Primes(long minimum, long maximum)
        {
            min = minimum;
            if (min < 2)
            {
                min = 2;
            }
            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;
                }
            }
        }
    }
}
========================================
这2段代码结果是一样的.区别是第一段代码支持IEnumerable接口,而第二段代码没有支持IEnumerable接口...
想问的是:
为什么不支持IEnumerable接口,怎么能实现IEnumerator GetEnumerator()方法?
这是一个例子,还好多不继承类也不支持接口都能实现他们的方法...我不理解?

解决方案 »

  1.   

    你可以自己区看看IEnumerable接口的内容,msdn上面应该有,印象中好像就是定义了IEnumerator GetEnumerator() 这个方法另外,接口可以理解为功能块的组合,并不是只有实现了接口才能使用这些功能块的
      

  2.   

    UP,找了我看的都是要支持IEnumerable接口才能实现IEnumerator GetEnumerator()方法...可是不支持IEnumerable也可以实现IEnumerator GetEnumerator()方法啊,为什么呢?纳闷.....
      

  3.   

    ojekleen
    我知道你说的意思,但是我是要Primes的一个事例迭代集合中的对象,代码如下:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        /// <summary>
        /// 
        /// </summary>
        class Program
        {
            static void Main(string[] args)
            {
                Primes primeForm1Tp1000 = new Primes(1, 1000);
                foreach (long i in primeForm1Tp1000)
                {
                    Console.Write("{0},", i);
                }
                Console.ReadKey();
            }
        }
    }
      

  4.   

    GetEnumerator(),  只是个函数罢了,只不过ienumable要求继承他的类必须实现这个函数
    不继承可以有这个函数也可以没有。
    并不有这个函数的类都是实现上述接口
      

  5.   

    7楼的,如果只是个函数的话,那函数名可以让我自己随便取吧?可是如果GetEnumerator()函数名字该为别的,将得到一个错误...
    9楼的,动态接口是什么?GOOGLE搜不到啊?
      

  6.   

    实现 IEnumerable 接口是为了支持foreach ,不实现就不支持foreach了?
    那么yield又是干什么的呢?!!
      

  7.   

    你们的意思是不是如果类A定义了GetEnumerator()这个函数,且用foreach in循环就代表类A继承了IEnumerable接口.不管有没有写上  class A:IEnumerable !!!!!
    是这个意思吗?
      

  8.   

    不对,
    实现IEnumerable接口的话,foreach可以
    Primes p;
    foreach long l in p
    {}
    不实现IEnumerable接口的话,通过yield return,foreach需要改成
    Primes p;
    foreach long l in p.GetEnumerator()//函数名字可以随便改
    {}另外,动态接口-Dynamic Interface,google上有的
      

  9.   

    楼上的,不写public class Primes : IEnumerable
    下面的代码一样可以用
    Primes primeForm1Tp1000 = new Primes(1, 1000); 
                foreach (long i in primeForm1Tp1000) 
                { 
                    Console.Write("{0},", i); 
                } 
    ============================
    而不需要你说的
    foreach long l in p.GetEnumerator()//函数名字可以随便改 
    {}
      

  10.   

    即使不实现IEnumerable,当你在使用foreach的时候其实内部还是在调用GetEnumerator方法,你可以把GetEnumerator方法名字改掉,就运行不了foreach了。
    试想,一个项目即使不用一个接口也能够实现,但是接口可以说是定义了一个规则或一个约束,所有继承自某接口的类都必须实现该接口的方法,这就给项目的设计带来了很多的灵活性,能够更多的控制项目(这就是为什么总是说要针对抽象编程,依赖倒置原则)。也就是说,如果该类是封装好的,即使不看其具体代码,只要知道它实现了IEnumerable接口,那么这个类就必定能用foreach,如果该类不实现该方法,则会报错。而如果不继承该接口,就不一定会有GetEnumerator方法,就必须关心类的具体内容,这就涉及了类的内部,破坏了类的封装性,因为即使不实现GetEnumerator方法,该类还是正确的,不会报错。
    其实对接口的理解也体现了对于面向对象的理解程度,相信搂主继续学下去就会体会到接口的神奇。
      

  11.   

    楼上的意思是说用foreach一定会调用GetEnumerator方法,不管过类有没支持IEnumerable接口,没有支持IEnumerable接口也行,只要他有GetEnumerator方法就不会报错,而支持IEnumerable接口的类一定能用foreach是吗?是这个意思吗?
      

  12.   

    对,因为实现了IEnumerable接口必定有GetEnumerator方法
      

  13.   

    不写实现IEnumerable接口确实也可以用,从来没想过,foreach还能这么用,汗,应该说是c#编译器比较强悍
    还有就是,yield return主要是用来返回一个IEnumerable接口,lz的代码里面却是返回IEnumerator接口,这种用法比较少,更常见的用法是:
            public static IEnumerable<int> GetEven(int min, int max)
            {
                for (int i = min; i < max; i++)
                {
                    if (i % 2 == 0)
                        yield return i;
                }
            }
      

  14.   

    yield return是2.0的新特性,使用起来比较方便
      

  15.   

    楼上的,IEnumerable接口就是定义了一个方法IEnumerator GetEnumerator(),返回的就是IEnumerator接口...
    我是个新手好多都不太懂...所以要好好学习...