C# .NET Framework 1.1 bug in Control class
I just encountered something really fishy in the .NET framework.
I am iterating over the Controls (Control.Controls Collection) of a derived control to effect some selection logic. When I iterative over this collection, I sporadically get the same reference twice ie: a foreach statement returns the same control multiple times instead of each individual control once.
This problem seems to be race-conditional as it does not occur if I break during the iteration. A stack trace shows that the iteration is in fact producing the same reference twice.
If I use a hashtable of references to the same objects instead of the Control.Controls collection this problem magically disappears! WOW!
Has anyone else seen this? Any comments from Microsoft land?
Like this...
using System.Collections;
....
public class MyControl : Control
{
IDictionary controls = new Hashtable()
.....
private void AddChildControl(Control child)
{
this.Controls.Add(child); //using the inherited collection
controls[GUID] = child; //using my own collection
}
.....
private void DoSelectionThingy()
{
//THIS DOESN'T WORK!!!!!!!!!
foreach(Control control in Controls)
{
IMyControl myControl = control as IMyControl
if(myControl != null)
{
myControl.Select = true;
}
}
//THIS DOES WORK!!!!!!!!!
foreach(Control control in controls)
{
IMyControl myControl = control as IMyControl
if(myControl != null)
{
myControl.Select = true;
}
}
.....
}

