T
Tin Gherdanarra
Dear mpdls,
here is a simple example of an IEnumerable that
generates integers:
It works, but I have only a vague idea of what's
going on. I understand that /yield/ wraps the
humble integer that comes from
counter++
into an IEnumerator<int>.
I don't understand why I have to implement TWO
GetEnumerator()-methods that both do the same and
only have different return types. Neither do I
understand which one is picked by /foreach/.
If I don't implement both GetEnumerator()s,
I get errors of omission.
IEnumerator is something like a /Generator/, right?
By the way, where is comp.lang.csharp?
Thanks
Tin
using System;
using System.Collections.Generic;
using System.Text;
namespace Riba
{
public class NumberEnumerable : IEnumerable<int>
{
private int counter = 0;
public IEnumerator<int> Core()
{
for (; ; )
{
yield return counter++;
}
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return Core();
}
public IEnumerator<int> GetEnumerator()
{
return Core();
}
}
class Program
{
static void Main(string[] args)
{
foreach (int i in new NumberEnumerable())
{
System.Console.WriteLine("{0}", i);
}
}
}
}
here is a simple example of an IEnumerable that
generates integers:
It works, but I have only a vague idea of what's
going on. I understand that /yield/ wraps the
humble integer that comes from
counter++
into an IEnumerator<int>.
I don't understand why I have to implement TWO
GetEnumerator()-methods that both do the same and
only have different return types. Neither do I
understand which one is picked by /foreach/.
If I don't implement both GetEnumerator()s,
I get errors of omission.
IEnumerator is something like a /Generator/, right?
By the way, where is comp.lang.csharp?
Thanks
Tin
using System;
using System.Collections.Generic;
using System.Text;
namespace Riba
{
public class NumberEnumerable : IEnumerable<int>
{
private int counter = 0;
public IEnumerator<int> Core()
{
for (; ; )
{
yield return counter++;
}
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return Core();
}
public IEnumerator<int> GetEnumerator()
{
return Core();
}
}
class Program
{
static void Main(string[] args)
{
foreach (int i in new NumberEnumerable())
{
System.Console.WriteLine("{0}", i);
}
}
}
}