Iterators in .NET 2.0 Nov '04 CTP

  • Thread starter Thread starter Mujtaba Syed
  • Start date Start date
M

Mujtaba Syed

I have the Nov '04 CTP of .NET 2.0 (The splash screen of Visual Studio .NET
calls it Beta 2).

I was trying some simple iterator code...

using System.Collections.Generic;

public class CityCollection : IEnumerable<string>
{
string[] m_Cities = {"New York","Paris","London"};
public IEnumerator<string> GetEnumerator()
{
for(int i = 0; i<m_Cities.Length; i++)
yield return m_Cities;
}
}

And I am getting a compile-time error:

test.cs(3,14): error CS0536: 'CityCollection' does not implement interface
member 'System.Collections.IEnumerable.GetEnumerator()'.
'CityCollection.GetEnumerator()' is either static, not public, or
has
the wrong return type.
c:\WINDOWS\Microsoft.NET\Framework\v2.0.41202\mscorlib.dll: (Location of
symbol
related to previous error)
test.cs(6,31): (Location of symbol related to previous error)

This code used to work in Beta 1! What's the problem?

Thanks,
Mujtaba.
 
Mujtaba,

FYI, there are newsgroups dedicated to VS 2005 and .NET 2.0 available
at
http://lab.msdn.microsoft.com/vs2005/community/newsgroups/default.aspx

This code used to work in Beta 1! What's the problem?

IEnumerable<T> now derives from the non-generic IEnumerable, so there
are two GetEnumerator methods for you to implement. Since they only
differ by return type you have to implement one of them explicitly. So
add for example

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}



Mattias
 
Back
Top