2005-12-19 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Mon, 19 Dec 2005 17:16:57 +0000 (17:16 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Mon, 19 Dec 2005 17:16:57 +0000 (17:16 -0000)
* Dictionary.cs: Fixed ICollection.CopyTo to use DictionaryEntry. Fixed
Key and Value CopyTo not to throw exception if the dictionary is empty
(fix bug #77019).

svn path=/trunk/mcs/; revision=54616

mcs/class/corlib/System.Collections.Generic/ChangeLog
mcs/class/corlib/System.Collections.Generic/Dictionary.cs

index e939fbd79df7a477dc901466fe19c7ebbd1b0932..0f65b8abe2d53450308923cd962e017518379f79 100644 (file)
@@ -1,5 +1,8 @@
-2005-12-19  Sebastien Pouliot  <sebastien@ximian.com>
-
+2005-12-19  Sebastien Pouliot  <sebastien@ximian.com> 
+       * Dictionary.cs: Fixed ICollection.CopyTo to use DictionaryEntry. Fixed
+       Key and Value CopyTo not to throw exception if the dictionary is empty
+       (fix bug #77019).
        * List.cs: Fix exception reporting to match MS behaviour (2.0 final).
 
 2005-11-19  Zoltan Varga  <vargaz@gmail.com>
index 2333231d50258a09b6d73fb70cd5c4dd7cb59a9c..d07cf590d1b2f25b29cc26ad3427416c3a614f8c 100644 (file)
@@ -442,9 +442,19 @@ namespace System.Collections.Generic {
 
                void ICollection.CopyTo (Array array, int index)
                {
-                       // TODO: Verify this can be a KeyValuePair, and doesn't need to be
-                       // a DictionaryEntry type
-                       CopyTo ((KeyValuePair<TKey, TValue> []) array, index);
+                       if (array == null)
+                               throw new ArgumentNullException ("array");
+                       if (index < 0)
+                               throw new ArgumentOutOfRangeException ("index");
+                       if (index >= array.Length)
+                               throw new ArgumentException ("index larger than largest valid index of array");
+                       if (array.Length - index < Count)
+                               throw new ArgumentException ("Destination array cannot hold the requested elements!");
+
+                       for (int i = 0; i < table.Length; ++i) {
+                               for (Slot slot = table [i]; slot != null; slot = slot.Next)
+                                       array.SetValue (new DictionaryEntry (slot.Data.Key, slot.Data.Value), index++);
+                       }
                }
 
                IEnumerator IEnumerable.GetEnumerator ()
@@ -574,6 +584,9 @@ namespace System.Collections.Generic {
                                        throw new ArgumentNullException ("array");
                                if (index < 0)
                                        throw new ArgumentOutOfRangeException ("index");
+                               // no exception for index==array.Length in this case
+                               if (dictionary.Count == 0)
+                                       return;
                                if (index >= array.Length)
                                        throw new ArgumentException ("index larger than largest valid index of array");
                                if (array.Length - index < dictionary.Count)
@@ -690,6 +703,9 @@ namespace System.Collections.Generic {
                                        throw new ArgumentNullException ("array");
                                if (index < 0)
                                        throw new ArgumentOutOfRangeException ("index");
+                               // no exception for index==array.Length in this case
+                               if (dictionary.Count == 0)
+                                       return;
                                if (index >= array.Length)
                                        throw new ArgumentException ("index larger than largest valid index of array");
                                if (array.Length - index < dictionary.Count)