Merge pull request #588 from Daniel15/bug-10872
[mono.git] / mcs / class / corlib / System.Collections.ObjectModel / KeyedCollection.cs
index 2cf9b5597f0953f334a7c3d948def16d66df312c..60b8d565a1b9c0e95a906ae5fceafbbc8332339d 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 using System;
 using System.Collections.Generic;
 using System.Runtime.InteropServices;
+using System.Diagnostics;
 
 namespace System.Collections.ObjectModel
 {
        [ComVisible(false)]
        [Serializable]
+       [DebuggerDisplay ("Count={Count}")]
+       [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]        
        public abstract class KeyedCollection<TKey, TItem> : Collection<TItem>
        {
                private Dictionary<TKey, TItem> dictionary;
@@ -158,26 +160,32 @@ namespace System.Collections.ObjectModel
 
                protected override void InsertItem (int index, TItem item)
                {
-                       if (dictionary != null)
-                       {
-                               dictionary.Add (GetKeyForItem (item), item);
-                       }
-                       else
-                       {
-                               if (dictionaryCreationThreshold != -1 && Count + 1 > dictionaryCreationThreshold)
-                               {
-                                       dictionary = new Dictionary<TKey, TItem> (comparer);
-
-                                       for (int i = Count - 1; i >= 0; i--)
-                                       {
-                                               TItem dictitem = this[i];
-                                               dictionary.Add(GetKeyForItem(dictitem), dictitem);
+                       TKey key = GetKeyForItem (item);
+                       if (key == null)
+                               throw new ArgumentNullException ("GetKeyForItem(item)");
+
+                       if (dictionary != null && dictionary.ContainsKey (key))
+                               throw new ArgumentException ("An element with the same key already exists in the dictionary.");
+
+                       if (dictionary == null)
+                               for (int i = 0; i < Count; ++i) {
+                                       if (comparer.Equals (key, GetKeyForItem (this [i]))) {
+                                               throw new ArgumentException ("An element with the same key already exists in the dictionary.");
                                        }
+                               }
+
+                       base.InsertItem (index, item);
 
-                                       dictionary.Add (GetKeyForItem (item), item);
+                       if (dictionary != null)
+                               dictionary.Add (key, item);
+                       else if (dictionaryCreationThreshold != -1 && Count > dictionaryCreationThreshold) {
+                               dictionary = new Dictionary<TKey, TItem> (comparer);
+
+                               for (int i = 0; i < Count; ++i) {
+                                       TItem dictitem = this [i];
+                                       dictionary.Add (GetKeyForItem (dictitem), dictitem);
                                }
                        }
-                       base.InsertItem (index, item);
                }
 
                protected override void RemoveItem (int index)
@@ -207,4 +215,3 @@ namespace System.Collections.ObjectModel
                }
        }
 }
-#endif