//.net framework 1.1public class DaysOfTheWeek : System.Collections.IEnumerable
{
string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < m_Days.Length; i++)
{
return m_Days[i]; //报错的位置
}
}
}class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek(); // Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
}
}//以下为错误提示:
//Iterators.cs(9,20): error CS0029: Cannot implicitly convert type 'string' to
// 'System.Collections.IEnumerator'
{
string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < m_Days.Length; i++)
{
return m_Days[i]; //报错的位置
}
}
}class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek(); // Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
}
}//以下为错误提示:
//Iterators.cs(9,20): error CS0029: Cannot implicitly convert type 'string' to
// 'System.Collections.IEnumerator'
{
return m_Days;
}
{
return m_Days.GetEnumerator();
}
// paths return a value//是不是因为我是.net framework 1.1的缘故,在2.0下或许可以通过?
{
string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; public System.Collections.IEnumerator GetEnumerator()
{
return m_Days.GetEnumerator(); //报错的位置
}
}class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek(); // Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
}
}