Dictionary/List code clean up/formatting (C#7) (#17196)
[platform/upstream/coreclr.git] / src / mscorlib / shared / System / Collections / Generic / Dictionary.cs
index b62d3f8..827cc24 100644 (file)
@@ -2,11 +2,10 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
-using System.Collections;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
 using System.Runtime.Serialization;
+using System.Threading;
 
 namespace System.Collections.Generic
 {
@@ -34,7 +33,7 @@ namespace System.Collections.Generic
     [DebuggerTypeProxy(typeof(IDictionaryDebugView<,>))]
     [DebuggerDisplay("Count = {Count}")]
     [Serializable]
-    [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
+    [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
     public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary, IReadOnlyDictionary<TKey, TValue>, ISerializable, IDeserializationCallback
     {
         private struct Entry
@@ -217,7 +216,7 @@ namespace System.Collections.Generic
                 int i = FindEntry(key);
                 if (i >= 0) return _entries[i].value;
                 ThrowHelper.ThrowKeyNotFoundException(key);
-                return default(TValue);
+                return default;
             }
             set
             {
@@ -233,9 +232,7 @@ namespace System.Collections.Generic
         }
 
         void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyValuePair)
-        {
-            Add(keyValuePair.Key, keyValuePair.Value);
-        }
+            => Add(keyValuePair.Key, keyValuePair.Value);
 
         bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
         {
@@ -274,9 +271,7 @@ namespace System.Collections.Generic
         }
 
         public bool ContainsKey(TKey key)
-        {
-            return FindEntry(key) >= 0;
-        }
+            => FindEntry(key) >= 0;
 
         public bool ContainsValue(TValue value)
         {
@@ -304,7 +299,7 @@ namespace System.Collections.Generic
                 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
             }
 
-            if (index < 0 || index > array.Length)
+            if ((uint)index > (uint)array.Length)
             {
                 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
             }
@@ -320,20 +315,16 @@ namespace System.Collections.Generic
             {
                 if (entries[i].hashCode >= 0)
                 {
-                    array[index++] = new KeyValuePair<TKey, TValue>(entries[i].key, entries[i].value);
+                    array[index + i] = new KeyValuePair<TKey, TValue>(entries[i].key, entries[i].value);
                 }
             }
         }
 
         public Enumerator GetEnumerator()
-        {
-            return new Enumerator(this, Enumerator.KeyValuePair);
-        }
+            => new Enumerator(this, Enumerator.KeyValuePair);
 
         IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
-        {
-            return new Enumerator(this, Enumerator.KeyValuePair);
-        }
+            => new Enumerator(this, Enumerator.KeyValuePair);
 
         public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
         {
@@ -585,8 +576,7 @@ namespace System.Collections.Generic
 
         public virtual void OnDeserialization(object sender)
         {
-            SerializationInfo siInfo;
-            HashHelpers.SerializationInfoTable.TryGetValue(this, out siInfo);
+            HashHelpers.SerializationInfoTable.TryGetValue(this, out SerializationInfo siInfo);
 
             if (siInfo == null)
             {
@@ -630,9 +620,7 @@ namespace System.Collections.Generic
         }
 
         private void Resize()
-        {
-            Resize(HashHelpers.ExpandPrime(_count), false);
-        }
+            => Resize(HashHelpers.ExpandPrime(_count), false);
 
         private void Resize(int newSize, bool forceNewHashCodes)
         {
@@ -711,11 +699,11 @@ namespace System.Collections.Generic
 
                         if (RuntimeHelpers.IsReferenceOrContainsReferences<TKey>())
                         {
-                            entry.key = default(TKey);
+                            entry.key = default;
                         }
                         if (RuntimeHelpers.IsReferenceOrContainsReferences<TValue>())
                         {
-                            entry.value = default(TValue);
+                            entry.value = default;
                         }
                         _freeList = i;
                         _freeCount++;
@@ -770,11 +758,11 @@ namespace System.Collections.Generic
 
                         if (RuntimeHelpers.IsReferenceOrContainsReferences<TKey>())
                         {
-                            entry.key = default(TKey);
+                            entry.key = default;
                         }
                         if (RuntimeHelpers.IsReferenceOrContainsReferences<TValue>())
                         {
-                            entry.value = default(TValue);
+                            entry.value = default;
                         }
                         _freeList = i;
                         _freeCount++;
@@ -786,7 +774,7 @@ namespace System.Collections.Generic
                     i = entry.next;
                 }
             }
-            value = default(TValue);
+            value = default;
             return false;
         }
 
@@ -798,63 +786,43 @@ namespace System.Collections.Generic
                 value = _entries[i].value;
                 return true;
             }
-            value = default(TValue);
+            value = default;
             return false;
         }
 
-        public bool TryAdd(TKey key, TValue value) => TryInsert(key, value, InsertionBehavior.None);
+        public bool TryAdd(TKey key, TValue value)
+            => TryInsert(key, value, InsertionBehavior.None);
 
-        bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
-        {
-            get { return false; }
-        }
+        bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
 
         void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
-        {
-            CopyTo(array, index);
-        }
+            => CopyTo(array, index);
 
         void ICollection.CopyTo(Array array, int index)
         {
             if (array == null)
-            {
                 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
-            }
-
             if (array.Rank != 1)
-            {
                 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
-            }
-
             if (array.GetLowerBound(0) != 0)
-            {
                 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
-            }
-
-            if (index < 0 || index > array.Length)
-            {
+            if ((uint)index > (uint)array.Length)
                 ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
-            }
-
             if (array.Length - index < Count)
-            {
                 ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
-            }
 
-            KeyValuePair<TKey, TValue>[] pairs = array as KeyValuePair<TKey, TValue>[];
-            if (pairs != null)
+            if (array is KeyValuePair<TKey, TValue>[] pairs)
             {
                 CopyTo(pairs, index);
             }
-            else if (array is DictionaryEntry[])
+            else if (array is DictionaryEntry[] dictEntryArray)
             {
-                DictionaryEntry[] dictEntryArray = array as DictionaryEntry[];
                 Entry[] entries = _entries;
                 for (int i = 0; i < _count; i++)
                 {
                     if (entries[i].hashCode >= 0)
                     {
-                        dictEntryArray[index++] = new DictionaryEntry(entries[i].key, entries[i].value);
+                        dictEntryArray[index + i] = new DictionaryEntry(entries[i].key, entries[i].value);
                     }
                 }
             }
@@ -874,7 +842,7 @@ namespace System.Collections.Generic
                     {
                         if (entries[i].hashCode >= 0)
                         {
-                            objects[index++] = new KeyValuePair<TKey, TValue>(entries[i].key, entries[i].value);
+                            objects[index + i] = new KeyValuePair<TKey, TValue>(entries[i].key, entries[i].value);
                         }
                     }
                 }
@@ -886,9 +854,7 @@ namespace System.Collections.Generic
         }
 
         IEnumerator IEnumerable.GetEnumerator()
-        {
-            return new Enumerator(this, Enumerator.KeyValuePair);
-        }
+            => new Enumerator(this, Enumerator.KeyValuePair);
 
         /// <summary>
         /// Ensures that the dictionary can hold up to 'capacity' entries without any further expansion of its backing storage
@@ -919,9 +885,7 @@ namespace System.Collections.Generic
         /// dictionary.TrimExcess();
         /// </summary>
         public void TrimExcess()
-        {
-            TrimExcess(Count);
-        }
+            => TrimExcess(Count);
 
         /// <summary>
         /// Sets the capacity of this dictionary to hold up 'capacity' entries without any further expansion of its backing storage
@@ -964,10 +928,7 @@ namespace System.Collections.Generic
             _freeCount = 0;
         }
 
-        bool ICollection.IsSynchronized
-        {
-            get { return false; }
-        }
+        bool ICollection.IsSynchronized => false;
 
         object ICollection.SyncRoot
         {
@@ -975,31 +936,19 @@ namespace System.Collections.Generic
             {
                 if (_syncRoot == null)
                 {
-                    System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
+                    Interlocked.CompareExchange<object>(ref _syncRoot, new object(), null);
                 }
                 return _syncRoot;
             }
         }
 
-        bool IDictionary.IsFixedSize
-        {
-            get { return false; }
-        }
+        bool IDictionary.IsFixedSize => false;
 
-        bool IDictionary.IsReadOnly
-        {
-            get { return false; }
-        }
+        bool IDictionary.IsReadOnly => false;
 
-        ICollection IDictionary.Keys
-        {
-            get { return (ICollection)Keys; }
-        }
+        ICollection IDictionary.Keys => (ICollection)Keys;
 
-        ICollection IDictionary.Values
-        {
-            get { return (ICollection)Values; }
-        }
+        ICollection IDictionary.Values => (ICollection)Values;
 
         object IDictionary.this[object key]
         {
@@ -1089,9 +1038,7 @@ namespace System.Collections.Generic
         }
 
         IDictionaryEnumerator IDictionary.GetEnumerator()
-        {
-            return new Enumerator(this, Enumerator.DictEntry);
-        }
+            => new Enumerator(this, Enumerator.DictEntry);
 
         void IDictionary.Remove(object key)
         {
@@ -1147,10 +1094,7 @@ namespace System.Collections.Generic
                 return false;
             }
 
-            public KeyValuePair<TKey, TValue> Current
-            {
-                get { return _current; }
-            }
+            public KeyValuePair<TKey, TValue> Current => _current;
 
             public void Dispose()
             {
@@ -1167,7 +1111,7 @@ namespace System.Collections.Generic
 
                     if (_getEnumeratorRetType == DictEntry)
                     {
-                        return new System.Collections.DictionaryEntry(_current.Key, _current.Value);
+                        return new DictionaryEntry(_current.Key, _current.Value);
                     }
                     else
                     {
@@ -1243,9 +1187,7 @@ namespace System.Collections.Generic
             }
 
             public Enumerator GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             public void CopyTo(TKey[] array, int index)
             {
@@ -1268,34 +1210,22 @@ namespace System.Collections.Generic
                 Entry[] entries = _dictionary._entries;
                 for (int i = 0; i < count; i++)
                 {
-                    if (entries[i].hashCode >= 0) array[index++] = entries[i].key;
+                    if (entries[i].hashCode >= 0) array[index + i] = entries[i].key;
                 }
             }
 
-            public int Count
-            {
-                get { return _dictionary.Count; }
-            }
+            public int Count => _dictionary.Count;
 
-            bool ICollection<TKey>.IsReadOnly
-            {
-                get { return true; }
-            }
+            bool ICollection<TKey>.IsReadOnly => true;
 
             void ICollection<TKey>.Add(TKey item)
-            {
-                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet);
-            }
+                => ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet);
 
             void ICollection<TKey>.Clear()
-            {
-                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet);
-            }
+                => ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet);
 
             bool ICollection<TKey>.Contains(TKey item)
-            {
-                return _dictionary.ContainsKey(item);
-            }
+                => _dictionary.ContainsKey(item);
 
             bool ICollection<TKey>.Remove(TKey item)
             {
@@ -1304,44 +1234,25 @@ namespace System.Collections.Generic
             }
 
             IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             IEnumerator IEnumerable.GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             void ICollection.CopyTo(Array array, int index)
             {
                 if (array == null)
-                {
                     ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
-                }
-
                 if (array.Rank != 1)
-                {
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
-                }
-
                 if (array.GetLowerBound(0) != 0)
-                {
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
-                }
-
-                if (index < 0 || index > array.Length)
-                {
+                if ((uint)index > (uint)array.Length)
                     ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
-                }
-
                 if (array.Length - index < _dictionary.Count)
-                {
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
-                }
 
-                TKey[] keys = array as TKey[];
-                if (keys != null)
+                if (array is TKey[] keys)
                 {
                     CopyTo(keys, index);
                 }
@@ -1359,7 +1270,7 @@ namespace System.Collections.Generic
                     {
                         for (int i = 0; i < count; i++)
                         {
-                            if (entries[i].hashCode >= 0) objects[index++] = entries[i].key;
+                            if (entries[i].hashCode >= 0) objects[index + i] = entries[i].key;
                         }
                     }
                     catch (ArrayTypeMismatchException)
@@ -1369,17 +1280,11 @@ namespace System.Collections.Generic
                 }
             }
 
-            bool ICollection.IsSynchronized
-            {
-                get { return false; }
-            }
+            bool ICollection.IsSynchronized => false;
 
-            object ICollection.SyncRoot
-            {
-                get { return ((ICollection)_dictionary).SyncRoot; }
-            }
+            object ICollection.SyncRoot => ((ICollection)_dictionary).SyncRoot;
 
-            public struct Enumerator : IEnumerator<TKey>, System.Collections.IEnumerator
+            public struct Enumerator : IEnumerator<TKey>, IEnumerator
             {
                 private Dictionary<TKey, TValue> _dictionary;
                 private int _index;
@@ -1391,7 +1296,7 @@ namespace System.Collections.Generic
                     _dictionary = dictionary;
                     _version = dictionary._version;
                     _index = 0;
-                    _currentKey = default(TKey);
+                    _currentKey = default;
                 }
 
                 public void Dispose()
@@ -1417,19 +1322,13 @@ namespace System.Collections.Generic
                     }
 
                     _index = _dictionary._count + 1;
-                    _currentKey = default(TKey);
+                    _currentKey = default;
                     return false;
                 }
 
-                public TKey Current
-                {
-                    get
-                    {
-                        return _currentKey;
-                    }
-                }
+                public TKey Current => _currentKey;
 
-                object System.Collections.IEnumerator.Current
+                object IEnumerator.Current
                 {
                     get
                     {
@@ -1442,7 +1341,7 @@ namespace System.Collections.Generic
                     }
                 }
 
-                void System.Collections.IEnumerator.Reset()
+                void IEnumerator.Reset()
                 {
                     if (_version != _dictionary._version)
                     {
@@ -1450,7 +1349,7 @@ namespace System.Collections.Generic
                     }
 
                     _index = 0;
-                    _currentKey = default(TKey);
+                    _currentKey = default;
                 }
             }
         }
@@ -1471,9 +1370,7 @@ namespace System.Collections.Generic
             }
 
             public Enumerator GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             public void CopyTo(TValue[] array, int index)
             {
@@ -1496,24 +1393,16 @@ namespace System.Collections.Generic
                 Entry[] entries = _dictionary._entries;
                 for (int i = 0; i < count; i++)
                 {
-                    if (entries[i].hashCode >= 0) array[index++] = entries[i].value;
+                    if (entries[i].hashCode >= 0) array[index + i] = entries[i].value;
                 }
             }
 
-            public int Count
-            {
-                get { return _dictionary.Count; }
-            }
+            public int Count => _dictionary.Count;
 
-            bool ICollection<TValue>.IsReadOnly
-            {
-                get { return true; }
-            }
+            bool ICollection<TValue>.IsReadOnly => true;
 
             void ICollection<TValue>.Add(TValue item)
-            {
-                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet);
-            }
+                => ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet);
 
             bool ICollection<TValue>.Remove(TValue item)
             {
@@ -1522,52 +1411,31 @@ namespace System.Collections.Generic
             }
 
             void ICollection<TValue>.Clear()
-            {
-                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet);
-            }
+                => ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet);
 
             bool ICollection<TValue>.Contains(TValue item)
-            {
-                return _dictionary.ContainsValue(item);
-            }
+                => _dictionary.ContainsValue(item);
 
             IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             IEnumerator IEnumerable.GetEnumerator()
-            {
-                return new Enumerator(_dictionary);
-            }
+                => new Enumerator(_dictionary);
 
             void ICollection.CopyTo(Array array, int index)
             {
                 if (array == null)
-                {
                     ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
-                }
-
                 if (array.Rank != 1)
-                {
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
-                }
-
                 if (array.GetLowerBound(0) != 0)
-                {
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
-                }
-
-                if (index < 0 || index > array.Length)
-                {
+                if ((uint)index > (uint)array.Length)
                     ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
-                }
-
                 if (array.Length - index < _dictionary.Count)
                     ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
 
-                TValue[] values = array as TValue[];
-                if (values != null)
+                if (array is TValue[] values)
                 {
                     CopyTo(values, index);
                 }
@@ -1585,7 +1453,7 @@ namespace System.Collections.Generic
                     {
                         for (int i = 0; i < count; i++)
                         {
-                            if (entries[i].hashCode >= 0) objects[index++] = entries[i].value;
+                            if (entries[i].hashCode >= 0) objects[index + i] = entries[i].value;
                         }
                     }
                     catch (ArrayTypeMismatchException)
@@ -1595,17 +1463,11 @@ namespace System.Collections.Generic
                 }
             }
 
-            bool ICollection.IsSynchronized
-            {
-                get { return false; }
-            }
+            bool ICollection.IsSynchronized => false;
 
-            object ICollection.SyncRoot
-            {
-                get { return ((ICollection)_dictionary).SyncRoot; }
-            }
+            object ICollection.SyncRoot => ((ICollection)_dictionary).SyncRoot;
 
-            public struct Enumerator : IEnumerator<TValue>, System.Collections.IEnumerator
+            public struct Enumerator : IEnumerator<TValue>, IEnumerator
             {
                 private Dictionary<TKey, TValue> _dictionary;
                 private int _index;
@@ -1617,7 +1479,7 @@ namespace System.Collections.Generic
                     _dictionary = dictionary;
                     _version = dictionary._version;
                     _index = 0;
-                    _currentValue = default(TValue);
+                    _currentValue = default;
                 }
 
                 public void Dispose()
@@ -1642,19 +1504,13 @@ namespace System.Collections.Generic
                         }
                     }
                     _index = _dictionary._count + 1;
-                    _currentValue = default(TValue);
+                    _currentValue = default;
                     return false;
                 }
 
-                public TValue Current
-                {
-                    get
-                    {
-                        return _currentValue;
-                    }
-                }
+                public TValue Current => _currentValue;
 
-                object System.Collections.IEnumerator.Current
+                object IEnumerator.Current
                 {
                     get
                     {
@@ -1667,14 +1523,14 @@ namespace System.Collections.Generic
                     }
                 }
 
-                void System.Collections.IEnumerator.Reset()
+                void IEnumerator.Reset()
                 {
                     if (_version != _dictionary._version)
                     {
                         ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
                     }
                     _index = 0;
-                    _currentValue = default(TValue);
+                    _currentValue = default;
                 }
             }
         }