J
Jim at FCSMO
[Apologies for the bad code formatting - such is posting on the Web.]
The following code produces an infinite loop in its second while loop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Foo
{
class Program
{
static void Main(string[] args)
{
var categories = new List<string> { "Meetings", "Coding", "Debugging", "Web
surfing" }.GetEnumerator();
while (categories.MoveNext())
{
Console.WriteLine(categories.Current);
}
var categories2 = new { cats = new List<string> { "Meetings", "Coding",
"Debugging", "Web surfing" }.GetEnumerator()};
while (categories2.cats.MoveNext())
{
Console.WriteLine(categories2.cats.Current);
}
}
}
}
This code fixes it:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Foo
{
class Program
{
static void Main(string[] args)
{
var categories = new List<string> { "Meetings", "Coding", "Debugging",
"Web surfing" }.GetEnumerator();
while (categories.MoveNext())
{
Console.WriteLine(categories.Current);
}
var categories2 = new { cats = new List<string> { "Meetings", "Coding",
"Debugging", "Web surfing" }.GetEnumerator() as IEnumerator<string> };
while (categories2.cats.MoveNext())
{
Console.WriteLine(categories2.cats.Current);
}
}
}
}
For a full write-up including diffs between the generated MSIL that explains
WHAT is happening and gives some other clues, see my blog post here -
http://ednortonengineeringsociety.blogspot.com/2008/02/earlier-i-had-posted-about-some-c.html
.. What I want to know is WHY it is happening? Education appreciated.
The following code produces an infinite loop in its second while loop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Foo
{
class Program
{
static void Main(string[] args)
{
var categories = new List<string> { "Meetings", "Coding", "Debugging", "Web
surfing" }.GetEnumerator();
while (categories.MoveNext())
{
Console.WriteLine(categories.Current);
}
var categories2 = new { cats = new List<string> { "Meetings", "Coding",
"Debugging", "Web surfing" }.GetEnumerator()};
while (categories2.cats.MoveNext())
{
Console.WriteLine(categories2.cats.Current);
}
}
}
}
This code fixes it:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Foo
{
class Program
{
static void Main(string[] args)
{
var categories = new List<string> { "Meetings", "Coding", "Debugging",
"Web surfing" }.GetEnumerator();
while (categories.MoveNext())
{
Console.WriteLine(categories.Current);
}
var categories2 = new { cats = new List<string> { "Meetings", "Coding",
"Debugging", "Web surfing" }.GetEnumerator() as IEnumerator<string> };
while (categories2.cats.MoveNext())
{
Console.WriteLine(categories2.cats.Current);
}
}
}
}
For a full write-up including diffs between the generated MSIL that explains
WHAT is happening and gives some other clues, see my blog post here -
http://ednortonengineeringsociety.blogspot.com/2008/02/earlier-i-had-posted-about-some-c.html
.. What I want to know is WHY it is happening? Education appreciated.