Foreach не работает с переменными типа IEnumerable - C#

Узнай цену своей работы

Формулировка задачи:

Добрый вечер. Пытаюсь разобраться с ошибкой: foreach не работает с переменными типа IEnumerable ,поскольку IEnumerable не содержит открытого определения для GetEnumerator. Код следующий:
            Queue arr = new Queue(s1, s2, s20, s19, s3, day1, month1, year1, s7, s8, s25, s27, s9, day2, month2, year2, s13, s14, s26, s28, s15, day3, month3, year3);

            foreach (Person1 b in arr.GetB(3))  //подчеркивает  foreach 
            {
               
            }
....
public class Queue
    {
        private Person1[] queue;
        static int i=0;
        
    public Queue(string s1, string  s2,string  s20, string s19,string s3,double day1, double month1,double year1,
        string s7, string s8, string s25, string s27, string s9, double day2, double month2, double year2, string s13, string s14, string s26, string s28, string s15, double day3, double month3, double year3)
    {
 
        queue = new Person1[] { new Person1(s1, s2, s20, s19, s3, day1, month1, year1) ,
        new Person1(s7, s8, s25, s27, s9, day2, month2, year2) ,
        new Person1(s13, s14, s26, s28, s15, day3, month3, year3) };
    }

    public IEnumerable GetB(int max)  //подчеркивает IEnumerable GetB и пишет:не может быть итератором,поскольку IEnumerable  //не явл.типом интерфейса итератора
    {
        for (int i = 0; i < max; i++)
           yield return queue[i];

    }

    }
В чем причина?Спасибо

Решение задачи: «Foreach не работает с переменными типа IEnumerable»

textual
Листинг программы
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace fd
{
    class Person1
    {
        private string Name { get; set; }
         public Person1(string name)
         {
             Name = name;
         }
         public override string ToString()
         {
             return Name;
         }
    }
    public class Queue
    {
        private Person1[] queue;
        static int i = 0;
 
        public Queue(string s1)
        {
            queue = new Person1[] 
            { 
                new Person1("ab") ,
                new Person1("ba") ,
                new Person1("abba") 
            };
        }
 
 
        public IEnumerable GetB(int max)  //подчеркивает IEnumerable GetB и пишет:не может быть итератором,поскольку IEnumerable  //не явл.типом интерфейса итератора
        {
            for (int i = 0; i < max; i++)
                yield return queue[i];
 
 
        }
 
 
 
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace fd
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue t=new Queue("hyukghj");
            foreach (var s in t.GetB(2))
            {
                Console.WriteLine(s);
            }
 
            Console.Read();
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.167 из 5
Похожие ответы