小弟C#新手,刚刚学习接口,不是很会使用,请大牛们帮忙看看如何解决错误。急!!!
下面是编译器显示的错误,错误位置在第9行,Backet1 “ConsoleApplication1.Backet”不实现接口成员“System.Collections.Generic.IEnumerable<ConsoleApplication1.Fruit>.GetEnumerator()”。“ConsoleApplication1.Backet.GetEnumerator()”无法实现“System.Collections.Generic.IEnumerable<ConsoleApplication1.Fruit>.GetEnumerator()”,因为它没有匹配的返回类型“System.Collections.Generic.IEnumerator<ConsoleApplication1.Fruit>”。 using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
    public class Backet : IEnumerable<Fruit>      
    {
        private Fruit[] fruit;
        private int ctr = 0;        public IEnumerator<string> GetEnumerator()
        {
            foreach (Fruit obj in fruit)
            {
                yield return obj.GetName();
                yield return obj.GetColor();
                yield return Convert.ToString(obj.GetWeight());
            }
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }        public Backet(params Fruit[] initialfruit)
        {
            fruit = new Fruit[10];
            foreach (Fruit obj in initialfruit)
            {
                fruit[ctr++] = obj;
            }
        }        public void add(Fruit obj)
        {
            fruit[ctr] = obj;
            ctr++;
        }        /*public string this[int index]
        {
            get
            {
                if (index < 0 || index >= backet.Length)
                {
                }
                return backet[index].;
            }
            set
            {
                strings[index] = value;
            }        }*/
        public int GetNumEntries()
        {
            return ctr;
        }
    }
    class Experiment
    {
        static void Main()
        {
            int operation;
            Backet backet = new Backet();
            Console.WriteLine("选择操作:1.放入 2.遍历");
            operation = Console.Read();
            if (operation == 1)
                AddFruit(backet);
            else
                Traversal(backet);
        }        public static void AddFruit(Backet backet)
        {
            string name;
            string color;
            int weight;
            Console.Write("输入名称:");
            name = Console.ReadLine();
            Console.Write("输入颜色:");
            color = Console.ReadLine();
            Console.Write("输入重量:");
            weight = Console.Read();
            if (name == "apple")
            {
                Apple obj = new Apple(name, color, weight);
                backet.add(obj);
            }
            else
            {
                Pear obj = new Pear(name, color, weight);
                backet.add(obj);
            }
        }
        public static void Traversal(Backet backet)
        {
            foreach (Fruit s in backet)
            {
                Console.WriteLine("Value: {0}", s);
            }
        }
    }
    
    public class Fruit
    {
        protected string name;
        protected string color;
        protected int weight;        public Fruit(string name, string color, int weight)
        {
            this.name = name;
            this.color = color;
            this.weight = weight;
        }        public string GetName()
        {
            return this.name;
        }        public string GetColor()
        {
            return this.color;
        }
        public int GetWeight()
        {
            return this.weight;
        }
    }    public class Apple : Fruit
    {
        public Apple(string name, string color, int weight):base(name,color,weight){}        
    }    public class Pear : Fruit
    {
        public Pear(string name, string color, int weight):base(name,color,weight){}  
    }
   
}

解决方案 »

  1.   

    下面这段:
      public IEnumerator<string> GetEnumerator()
      {
      foreach (Fruit obj in fruit)
      {
      yield return obj.GetName();
      yield return obj.GetColor();
      yield return Convert.ToString(obj.GetWeight());
      }
      }
    改成:
    public IEnumerator<Fruit> GetEnumerator()
    {
    foreach (Fruit obj in fruit)
    yield return obj;
    }
      

  2.   

    public IEnumerator<Fruit> GetEnumerator()
      {
      foreach (Fruit obj in fruit)
      {
      yield return obj;
      }
    返回类型