// 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
{
[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
int i = FindEntry(key);
if (i >= 0) return _entries[i].value;
ThrowHelper.ThrowKeyNotFoundException(key);
- return default(TValue);
+ return default;
}
set
{
}
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)
{
}
public bool ContainsKey(TKey key)
- {
- return FindEntry(key) >= 0;
- }
+ => FindEntry(key) >= 0;
public bool ContainsValue(TValue value)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}
- if (index < 0 || index > array.Length)
+ if ((uint)index > (uint)array.Length)
{
ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
}
{
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)
{
public virtual void OnDeserialization(object sender)
{
- SerializationInfo siInfo;
- HashHelpers.SerializationInfoTable.TryGetValue(this, out siInfo);
+ HashHelpers.SerializationInfoTable.TryGetValue(this, out SerializationInfo siInfo);
if (siInfo == null)
{
}
private void Resize()
- {
- Resize(HashHelpers.ExpandPrime(_count), false);
- }
+ => Resize(HashHelpers.ExpandPrime(_count), false);
private void Resize(int newSize, bool forceNewHashCodes)
{
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++;
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++;
i = entry.next;
}
}
- value = default(TValue);
+ value = default;
return false;
}
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);
}
}
}
{
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);
}
}
}
}
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
/// 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
_freeCount = 0;
}
- bool ICollection.IsSynchronized
- {
- get { return false; }
- }
+ bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot
{
{
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]
{
}
IDictionaryEnumerator IDictionary.GetEnumerator()
- {
- return new Enumerator(this, Enumerator.DictEntry);
- }
+ => new Enumerator(this, Enumerator.DictEntry);
void IDictionary.Remove(object key)
{
return false;
}
- public KeyValuePair<TKey, TValue> Current
- {
- get { return _current; }
- }
+ public KeyValuePair<TKey, TValue> Current => _current;
public void Dispose()
{
if (_getEnumeratorRetType == DictEntry)
{
- return new System.Collections.DictionaryEntry(_current.Key, _current.Value);
+ return new DictionaryEntry(_current.Key, _current.Value);
}
else
{
}
public Enumerator GetEnumerator()
- {
- return new Enumerator(_dictionary);
- }
+ => new Enumerator(_dictionary);
public void CopyTo(TKey[] array, int index)
{
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)
{
}
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);
}
{
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)
}
}
- 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;
_dictionary = dictionary;
_version = dictionary._version;
_index = 0;
- _currentKey = default(TKey);
+ _currentKey = default;
}
public void Dispose()
}
_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
{
}
}
- void System.Collections.IEnumerator.Reset()
+ void IEnumerator.Reset()
{
if (_version != _dictionary._version)
{
}
_index = 0;
- _currentKey = default(TKey);
+ _currentKey = default;
}
}
}
}
public Enumerator GetEnumerator()
- {
- return new Enumerator(_dictionary);
- }
+ => new Enumerator(_dictionary);
public void CopyTo(TValue[] array, int index)
{
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)
{
}
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);
}
{
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)
}
}
- 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;
_dictionary = dictionary;
_version = dictionary._version;
_index = 0;
- _currentValue = default(TValue);
+ _currentValue = default;
}
public void Dispose()
}
}
_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
{
}
}
- void System.Collections.IEnumerator.Reset()
+ void IEnumerator.Reset()
{
if (_version != _dictionary._version)
{
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion();
}
_index = 0;
- _currentValue = default(TValue);
+ _currentValue = default;
}
}
}