首页 新闻 赞助 找找看

请问这个实例代码错了,要怎么修改?

0
[已解决问题] 解决于 2021-06-03 14:05

报错“
错误 CS1579 “Spect”不包含“GetEnumerator”的公共实例或扩展定义,因此 foreach 语句不能作用于“Spect”类型的变量 ”
要怎么修改,菜鸟,请大家指点
using System;
using System.Collections.Generic;

namespace ConsoleApp20
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Spect spuv = new Spect(true);
Spect spir = new Spect(false);
foreach (string co in spuv)
{
Console.WriteLine($"{ co}");
}
Console.WriteLine("********");
foreach (string sir in spir)
Console.WriteLine($"{sir}");
}
}
class Spect
{
bool listFromuvToir;
string[] colors = { "yellow", "red", "black", "gray" };
public Spect(bool lutr)
{
listFromuvToir = lutr;
}
public IEnumerator<string> GetEnumrator()
{
return listFromuvToir ? UVTOIR : IRTOUR;
}
public IEnumerator<string> UVTOIR
{
get
{
for (int i = 0; i < colors.Length; i++)
{
yield return colors[i];
}
}
}

    public IEnumerator<string> IRTOUR
    {
        get
        {
            for (int j = colors.Length - 1; j >= 0; j--)
            {
                yield return colors[j];
            }
        }
    }
}

}

herry507的主页 herry507 | 菜鸟二级 | 园豆:243
提问于:2021-05-16 22:33
< >
分享
最佳答案
0

GetEnumrator拼錯了,這樣才對

public IEnumerator<string> GetEnumerator()
奖励园豆:5
RosonJ | 老鸟四级 |园豆:4910 | 2021-05-17 10:57

也没报错,晕,感谢,

herry507 | 园豆:243 (菜鸟二级) | 2021-05-17 23:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册