Iterators in .NET 2.0 Nov '04 CTP

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.
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top