1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Diagnostics;
10 namespace System.Runtime.InteropServices.WindowsRuntime
12 [DebuggerDisplay("Count = {Count}")]
13 internal sealed class DictionaryKeyCollection<TKey, TValue> : ICollection<TKey>
15 private readonly IDictionary<TKey, TValue> dictionary;
17 public DictionaryKeyCollection(IDictionary<TKey, TValue> dictionary)
19 if (dictionary == null)
20 throw new ArgumentNullException(nameof(dictionary));
22 this.dictionary = dictionary;
25 public void CopyTo(TKey[] array, int index)
28 throw new ArgumentNullException(nameof(array));
30 throw new ArgumentOutOfRangeException(nameof(index));
31 if (array.Length <= index && this.Count > 0)
32 throw new ArgumentException(SR.Arg_IndexOutOfRangeException);
33 if (array.Length - index < dictionary.Count)
34 throw new ArgumentException(SR.Argument_InsufficientSpaceToCopyCollection);
37 foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
39 array[i++] = mapping.Key;
45 get { return dictionary.Count; }
48 bool ICollection<TKey>.IsReadOnly
53 void ICollection<TKey>.Add(TKey item)
55 throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
58 void ICollection<TKey>.Clear()
60 throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
63 public bool Contains(TKey item)
65 return dictionary.ContainsKey(item);
68 bool ICollection<TKey>.Remove(TKey item)
70 throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
73 IEnumerator IEnumerable.GetEnumerator()
75 return ((IEnumerable<TKey>)this).GetEnumerator();
78 public IEnumerator<TKey> GetEnumerator()
80 return new DictionaryKeyEnumerator<TKey, TValue>(dictionary);
82 } // public class DictionaryKeyCollection<TKey, TValue>
85 internal sealed class DictionaryKeyEnumerator<TKey, TValue> : IEnumerator<TKey>
87 private readonly IDictionary<TKey, TValue> dictionary;
88 private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
90 public DictionaryKeyEnumerator(IDictionary<TKey, TValue> dictionary)
92 if (dictionary == null)
93 throw new ArgumentNullException(nameof(dictionary));
95 this.dictionary = dictionary;
96 enumeration = dictionary.GetEnumerator();
99 void IDisposable.Dispose()
101 enumeration.Dispose();
104 public bool MoveNext()
106 return enumeration.MoveNext();
109 object? IEnumerator.Current
111 get { return ((IEnumerator<TKey>)this).Current; }
116 get { return enumeration.Current.Key; }
121 enumeration = dictionary.GetEnumerator();
123 } // class DictionaryKeyEnumerator<TKey, TValue>
126 // DictionaryKeyCollection.cs