[Mono-bugs] [Bug 75073][Nor] New - wrong type in generic dictionary foreach

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Fri May 27 16:44:34 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 dsilva at ccs.neu.edu.

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

--- shadow/75073	2005-05-27 16:44:34.000000000 -0400
+++ shadow/75073.tmp.28007	2005-05-27 16:44:34.000000000 -0400
@@ -0,0 +1,86 @@
+Bug#: 75073
+Product: Mono: Compilers
+Version: 1.1
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: C#
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: dsilva at ccs.neu.edu               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: wrong type in generic dictionary foreach
+
+Description of Problem:
+
+This program with a generic IDictionary compiles fine:
+
+using System.Collections.Generic;
+
+public class App {
+  public static void Main() {
+    IDictionary<string, int> values = new Dictionary<string, int>();
+    values["one"] = 1; values["two"] = 2;
+
+    foreach (string key in values.Keys)
+      System.Console.WriteLine("key: {0}", key);
+  }
+}
+
+
+An equivalent program with the keys lifted out of the foreach compiles too:
+
+using System.Collections.Generic;
+
+public class App {
+  public static void Main() {
+    IDictionary<string, int> values = new Dictionary<string, int>();
+    values["one"] = 1; values["two"] = 2;
+
+    ICollection<string> keys = values.Keys;
+    foreach (string key in keys)
+      System.Console.WriteLine("key: {0}", key);
+  }
+}
+
+
+The same program with Dictionary instead of IDictionary as the static type
+compiles as well:
+
+using System.Collections.Generic;
+
+public class App {
+  public static void Main() {
+    Dictionary<string, int> values = new Dictionary<string, int>();
+    values["one"] = 1; values["two"] = 2;
+
+    ICollection<string> keys = values.Keys;
+    foreach (string key in keys)
+      System.Console.WriteLine("key: {0}", key);
+  }
+}
+
+
+The equivalent program with the keys in the foreach does not compile though:
+
+using System.Collections.Generic;
+
+public class App {
+  public static void Main() {
+    Dictionary<string, int> values = new Dictionary<string, int>();
+    values["one"] = 1; values["two"] = 2;
+
+    foreach (string key in values.Keys)
+      System.Console.WriteLine("key: {0}", key);
+  }
+}
+
+$ gmcs keys.cs
+keys.cs(9) error CS0030: Cannot convert type
+'System.Collections.Generic.KeyValuePair`2' to 'string'
+Compilation failed: 1 error(s), 0 warnings


More information about the mono-bugs mailing list