T
Tony Johansson
Hi!
At the end is a complete short program listed.
If I do this statement
Stack<string>.Enumerator q = numbers.GetEnumerator();
I create a Enumerator that is called q in this example.
But enumerator is used when iterating through a collection for example by
using the foreach construction.
So my first simple question is why is it not possible to write foreach in
this way using q as an enumerator ???
This cause the compile that is listed after the foreach.
My qualified guess is because this enumerator q doesn't implement the
IEnumerable we can't use a foreach here. That is what the compile errror
says. I'm on the right track.
foreach(string s in q)
{
Console.WriteLine(s);
}
Error 1 foreach statement cannot operate on variables of type
'System.Collections.Generic.Stack<string>.Enumerator' because
'System.Collections.Generic.Stack<string>.Enumerator' does not contain a
public definition for 'GetEnumerator' F:\C#\ConsoleApplication3\Tester.cs 42
7 ConsoleApplication3
My second question is:
This foreach construction in main works fine and that is because this
numbers(Stack<T>) implement the IEnumerable interface.
Is that correct described of me!
foreach (string number in numbers)
{
Console.WriteLine(number);
}
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
Stack<string>.Enumerator q = numbers.GetEnumerator();
while (q.MoveNext())
Console.WriteLine(q.Current);
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine("Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next: '{0}'", numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next: '{0}'", numbers.Peek());
}
//Tony
At the end is a complete short program listed.
If I do this statement
Stack<string>.Enumerator q = numbers.GetEnumerator();
I create a Enumerator that is called q in this example.
But enumerator is used when iterating through a collection for example by
using the foreach construction.
So my first simple question is why is it not possible to write foreach in
this way using q as an enumerator ???
This cause the compile that is listed after the foreach.
My qualified guess is because this enumerator q doesn't implement the
IEnumerable we can't use a foreach here. That is what the compile errror
says. I'm on the right track.
foreach(string s in q)
{
Console.WriteLine(s);
}
Error 1 foreach statement cannot operate on variables of type
'System.Collections.Generic.Stack<string>.Enumerator' because
'System.Collections.Generic.Stack<string>.Enumerator' does not contain a
public definition for 'GetEnumerator' F:\C#\ConsoleApplication3\Tester.cs 42
7 ConsoleApplication3
My second question is:
This foreach construction in main works fine and that is because this
numbers(Stack<T>) implement the IEnumerable interface.
Is that correct described of me!
foreach (string number in numbers)
{
Console.WriteLine(number);
}
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
Stack<string>.Enumerator q = numbers.GetEnumerator();
while (q.MoveNext())
Console.WriteLine(q.Current);
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine("Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next: '{0}'", numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next: '{0}'", numbers.Peek());
}
//Tony