报错“
错误 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];
}
}
}
}
}
GetEnumrator拼錯了,這樣才對
public IEnumerator<string> GetEnumerator()
也没报错,晕,感谢,