//.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'

解决方案 »

  1.   

    public System.Collections.IEnumerator GetEnumerator()
        {
                       return m_Days;
               }
      

  2.   

    楼上的老大,这么改还是编译通不过啊。报错的提示就改了一个地方,其余照旧,就是string变成了string[]。
      

  3.   

    public System.Collections.IEnumerator GetEnumerator()
        {
                       return m_Days.GetEnumerator();
               }
      

  4.   

    这个方法就是实现GetEnumerator,在方法体中有调用GetEnumerator,难道是递归,还是没有通过。//Iterators.cs(5,43): error CS0161: 'DaysOfTheWeek.GetEnumerator()': not all code
    //        paths return a value//是不是因为我是.net framework 1.1的缘故,在2.0下或许可以通过?
      

  5.   

    public class DaysOfTheWeek : System.Collections.IEnumerable
    {
    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 + " ");
    }
    }
    }