IEnumerable<T>

  • Thread starter Thread starter Gawel
  • Start date Start date
G

Gawel

Hajo,

compiling below code I get following error:
Error 1 'ServerTreeConsoleApplication.ServerControlsTree' does not
implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'ServerTreeConsoleApplication.ServerControlsTree.GetEnumerator()' is
either static, not public, or has the wrong return
type. E:\Work\Trials\ServerTreeConsoleApplication\ServerTreeConsoleApplication\ServerControlsTree.cs 7 18 ServerTreeConsoleApplication

Can someone explain me why?

thanks in advance for info

Gawel

public class ServerControlsTree : IEnumerable<ServerControl>
{
private ServerControl _root;

public ServerControl Root
{
get { return _root; }
set { _root = value; }
}

private ServerControl _enumerationRoot;

public ServerControl EnumerationRoot
{
get { return _enumerationRoot; }
set { _enumerationRoot = value; }
}

public IEnumerator<ServerControl> GetEnumerator()
{
List<ServerControl> controls = new List<ServerControl>();
controls.Add(_root);

for (int i = 0; i < controls.Count; i++)
{
yield return controls;

foreach (ServerControl childControl in
controls.Children)
{
controls.Add(childControl);
}
}
}
}
 
Can someone explain me why?

Because IEnumerable<T> derives from the non-generic IEnumerable, so
there are two GetEnumerator methods you have to implement. The easiest
way is to add

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



Mattias
 
Back
Top