1 ///----------- ----------- ----------- ----------- ----------- -----------
2 /// <copyright file="DictionaryKeyCollection.cs" company="Microsoft">
3 /// Copyright (c) Microsoft Corporation. All rights reserved.
6 /// <owner>gpaperin</owner>
7 ///----------- ----------- ----------- ----------- ----------- -----------
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Diagnostics;
14 namespace System.Runtime.InteropServices.WindowsRuntime
17 [DebuggerDisplay("Count = {Count}")]
18 internal sealed class DictionaryKeyCollection<TKey, TValue> : ICollection<TKey>
20 private readonly IDictionary<TKey, TValue> dictionary;
22 public DictionaryKeyCollection(IDictionary<TKey, TValue> dictionary)
24 if (dictionary == null)
25 throw new ArgumentNullException("dictionary");
27 this.dictionary = dictionary;
30 public void CopyTo(TKey[] array, int index)
33 throw new ArgumentNullException("array");
35 throw new ArgumentOutOfRangeException("index");
36 if (array.Length <= index && this.Count > 0)
37 throw new ArgumentException(Environment.GetResourceString("Arg_IndexOutOfRangeException"));
38 if (array.Length - index < dictionary.Count)
39 throw new ArgumentException(Environment.GetResourceString("Argument_InsufficientSpaceToCopyCollection"));
42 foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
44 array[i++] = mapping.Key;
49 get { return dictionary.Count; }
52 bool ICollection<TKey>.IsReadOnly {
56 void ICollection<TKey>.Add(TKey item)
58 throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
61 void ICollection<TKey>.Clear()
63 throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
66 public bool Contains(TKey item)
68 return dictionary.ContainsKey(item);
71 bool ICollection<TKey>.Remove(TKey item)
73 throw new NotSupportedException(Environment.GetResourceString("NotSupported_KeyCollectionSet"));
76 IEnumerator IEnumerable.GetEnumerator()
78 return ((IEnumerable<TKey>)this).GetEnumerator();
81 public IEnumerator<TKey> GetEnumerator()
83 return new DictionaryKeyEnumerator<TKey, TValue>(dictionary);
85 } // public class DictionaryKeyCollection<TKey, TValue>
89 internal sealed class DictionaryKeyEnumerator<TKey, TValue> : IEnumerator<TKey>
91 private readonly IDictionary<TKey, TValue> dictionary;
92 private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
94 public DictionaryKeyEnumerator(IDictionary<TKey, TValue> dictionary)
96 if (dictionary == null)
97 throw new ArgumentNullException("dictionary");
99 this.dictionary = dictionary;
100 this.enumeration = dictionary.GetEnumerator();
103 void IDisposable.Dispose()
105 enumeration.Dispose();
108 public bool MoveNext()
110 return enumeration.MoveNext();
113 Object IEnumerator.Current {
114 get { return ((IEnumerator<TKey>)this).Current; }
117 public TKey Current {
118 get { return enumeration.Current.Key; }
123 enumeration = dictionary.GetEnumerator();
125 } // class DictionaryKeyEnumerator<TKey, TValue>
128 // DictionaryKeyCollection.cs