Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\ArraySortHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\Comparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\Dictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\DictionaryEnumerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\EqualityComparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\IAsyncEnumerable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\IAsyncEnumerator.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ private void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
}

public Enumerator GetEnumerator()
=> new Enumerator(this, Enumerator.KeyValuePair);
=> new Enumerator(this);

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
=> new Enumerator(this, Enumerator.KeyValuePair);
=> new Enumerator(this);

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
Expand Down Expand Up @@ -976,7 +976,7 @@ void ICollection.CopyTo(Array array, int index)
}

IEnumerator IEnumerable.GetEnumerator()
=> new Enumerator(this, Enumerator.KeyValuePair);
=> new Enumerator(this);

/// <summary>
/// Ensures that the dictionary can hold up to 'capacity' entries without any further expansion of its backing storage
Expand Down Expand Up @@ -1152,7 +1152,7 @@ bool IDictionary.Contains(object key)
}

IDictionaryEnumerator IDictionary.GetEnumerator()
=> new Enumerator(this, Enumerator.DictEntry);
=> new DictionaryEnumerator(this);

void IDictionary.Remove(object key)
{
Expand All @@ -1173,24 +1173,18 @@ private ref int GetBucket(uint hashCode)
#endif
}

public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>,
IDictionaryEnumerator
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
{
private readonly Dictionary<TKey, TValue> _dictionary;
private readonly int _version;
private int _index;
private KeyValuePair<TKey, TValue> _current;
private readonly int _getEnumeratorRetType; // What should Enumerator.Current return?

internal const int DictEntry = 1;
internal const int KeyValuePair = 2;

internal Enumerator(Dictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
internal Enumerator(Dictionary<TKey, TValue> dictionary)
{
_dictionary = dictionary;
_version = dictionary._version;
_index = 0;
_getEnumeratorRetType = getEnumeratorRetType;
_current = default;
}

Expand Down Expand Up @@ -1234,14 +1228,7 @@ public void Dispose()
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
}

if (_getEnumeratorRetType == DictEntry)
{
return new DictionaryEntry(_current.Key, _current.Value);
}
else
{
return new KeyValuePair<TKey, TValue>(_current.Key, _current.Value);
}
return _current;
}
}

Expand All @@ -1255,45 +1242,6 @@ void IEnumerator.Reset()
_index = 0;
_current = default;
}

DictionaryEntry IDictionaryEnumerator.Entry
{
get
{
if (_index == 0 || (_index == _dictionary._count + 1))
{
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
}

return new DictionaryEntry(_current.Key, _current.Value);
}
}

object IDictionaryEnumerator.Key
{
get
{
if (_index == 0 || (_index == _dictionary._count + 1))
{
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
}

return _current.Key;
}
}

object? IDictionaryEnumerator.Value
{
get
{
if (_index == 0 || (_index == _dictionary._count + 1))
{
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
}

return _current.Value;
}
}
}

[DebuggerTypeProxy(typeof(DictionaryKeyCollectionDebugView<,>))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Collections.Generic
{
internal interface IKeyValue
{
public object? Key { get; }
public object? Value { get; }
}

internal class DictionaryEnumerator : IDictionaryEnumerator
{
private readonly IEnumerator _enumerator;
private IKeyValue? _current;

public DictionaryEnumerator(IEnumerable enumerable)
{
_enumerator = enumerable.GetEnumerator();
_current = null;
}

public object Key => _current?.Key!;

public object? Value => _current?.Value;

public DictionaryEntry Entry => new DictionaryEntry(_current?.Key!, _current?.Value);

public object? Current => _current;

public bool MoveNext()
{
bool result = _enumerator.MoveNext();
_current = (IKeyValue)_enumerator.Current!;
return result;
}

public void Reset() => _enumerator.Reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal static string PairToString(object? key, object? value)
// and IReadOnlyDictionary<TKey, TValue>.
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public readonly struct KeyValuePair<TKey, TValue>
public readonly struct KeyValuePair<TKey, TValue> : IKeyValue
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly TKey key; // Do not rename (binary serialization)
Expand All @@ -66,6 +66,10 @@ public KeyValuePair(TKey key, TValue value)

public TValue Value => value;

object? IKeyValue.Key => key;

object? IKeyValue.Value => value;

public override string ToString()
{
return KeyValuePair.PairToString(Key, Value);
Expand Down