[Mono-bugs] [Bug 75689][Nor] New - another anonymous delegate bug

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Tue Aug 2 11:46:40 EDT 2005


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by danw at novell.com.

http://bugzilla.ximian.com/show_bug.cgi?id=75689

--- shadow/75689	2005-08-02 11:46:40.000000000 -0400
+++ shadow/75689.tmp.17483	2005-08-02 11:46:40.000000000 -0400
@@ -0,0 +1,67 @@
+Bug#: 75689
+Product: Mono: Compilers
+Version: 1.1
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: C#
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: danw at novell.com               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: another anonymous delegate bug
+
+using System;
+	using System.Collections;
+	
+	public class Foo {
+		int n;
+		public Foo (int n) { this.n = n; }
+		public void Print (string msg) { Console.WriteLine (n); }
+	}
+	
+	public class AnonDelegateTest {
+	
+		public delegate void MyDelegate ();
+	
+		public static ArrayList delegates = new ArrayList ();
+	
+		public static void Setup (string msg)
+		{
+			Foo[] foos = new Foo[] { new Foo (1), new Foo (2), new Foo (3), new Foo
+(4) };
+	
+			Console.WriteLine (msg);
+	
+			foreach (Foo foo in foos) {
+				MyDelegate deleg = delegate () { foo.Print (msg); };
+				delegates.Add (deleg);
+			}
+		}
+	
+		public static void Main ()
+		{
+			Setup ("foo");
+			foreach (MyDelegate deleg in delegates)
+				deleg ();
+		}
+	}
+
+Running this outputs:
+foo
+4
+4
+4
+4
+
+If you make various changes, such as commenting out the line
+"Console.WriteLine (msg);", it will correctly output 1 2 3 4
+instead.
+
+This isn't specific to foreach(). It says 4 4 4 4 with a for
+loop too.


More information about the mono-bugs mailing list