Dictionary Initalize CQ (#15461)
authorBen Adams <thundercat@illyriad.co.uk>
Mon, 11 Dec 2017 15:02:20 +0000 (15:02 +0000)
committerJan Kotas <jkotas@microsoft.com>
Mon, 11 Dec 2017 15:02:20 +0000 (10:02 -0500)
src/mscorlib/shared/System/Collections/Generic/Dictionary.cs

index 6bd0785..4f34275 100644 (file)
@@ -376,10 +376,15 @@ namespace System.Collections.Generic
         private void Initialize(int capacity)
         {
             int size = HashHelpers.GetPrime(capacity);
-            _buckets = new int[size];
-            for (int i = 0; i < _buckets.Length; i++) _buckets[i] = -1;
-            _entries = new Entry[size];
+            int[] buckets = new int[size];
+            for (int i = 0; i < buckets.Length; i++)
+            {
+                buckets[i] = -1;
+            }
+
             _freeList = -1;
+            _buckets = buckets;
+            _entries = new Entry[size];
         }
 
         private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
@@ -470,10 +475,7 @@ namespace System.Collections.Generic
 
             if (hashsize != 0)
             {
-                _buckets = new int[hashsize];
-                for (int i = 0; i < _buckets.Length; i++) _buckets[i] = -1;
-                _entries = new Entry[hashsize];
-                _freeList = -1;
+                Initialize(hashsize);
 
                 KeyValuePair<TKey, TValue>[] array = (KeyValuePair<TKey, TValue>[])
                     siInfo.GetValue(KeyValuePairsName, typeof(KeyValuePair<TKey, TValue>[]));