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;
9 using System.Runtime.InteropServices;
10 using System.Runtime.InteropServices.WindowsRuntime;
13 namespace System.Runtime.InteropServices.WindowsRuntime
15 [DebuggerDisplay("Count = {Count}")]
16 internal sealed class DictionaryValueCollection<TKey, TValue> : ICollection<TValue>
18 private readonly IDictionary<TKey, TValue> dictionary;
20 public DictionaryValueCollection(IDictionary<TKey, TValue> dictionary)
22 if (dictionary == null)
23 throw new ArgumentNullException(nameof(dictionary));
25 this.dictionary = dictionary;
28 public void CopyTo(TValue[] array, int index)
31 throw new ArgumentNullException(nameof(array));
33 throw new ArgumentOutOfRangeException(nameof(index));
34 if (array.Length <= index && this.Count > 0)
35 throw new ArgumentException(SR.Arg_IndexOutOfRangeException);
36 if (array.Length - index < dictionary.Count)
37 throw new ArgumentException(SR.Argument_InsufficientSpaceToCopyCollection);
40 foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
42 array[i++] = mapping.Value;
48 get { return dictionary.Count; }
51 bool ICollection<TValue>.IsReadOnly
56 void ICollection<TValue>.Add(TValue item)
58 throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
61 void ICollection<TValue>.Clear()
63 throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
66 public bool Contains(TValue item)
68 EqualityComparer<TValue> comparer = EqualityComparer<TValue>.Default;
69 foreach (TValue value in this)
70 if (comparer.Equals(item, value))
75 bool ICollection<TValue>.Remove(TValue item)
77 throw new NotSupportedException(SR.NotSupported_ValueCollectionSet);
80 IEnumerator IEnumerable.GetEnumerator()
82 return ((IEnumerable<TValue>)this).GetEnumerator();
85 public IEnumerator<TValue> GetEnumerator()
87 return new DictionaryValueEnumerator<TKey, TValue>(dictionary);
89 } // public class DictionaryValueCollection<TKey, TValue>
92 internal sealed class DictionaryValueEnumerator<TKey, TValue> : IEnumerator<TValue>
94 private readonly IDictionary<TKey, TValue> dictionary;
95 private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
97 public DictionaryValueEnumerator(IDictionary<TKey, TValue> dictionary)
99 if (dictionary == null)
100 throw new ArgumentNullException(nameof(dictionary));
102 this.dictionary = dictionary;
103 enumeration = dictionary.GetEnumerator();
106 void IDisposable.Dispose()
108 enumeration.Dispose();
111 public bool MoveNext()
113 return enumeration.MoveNext();
116 object IEnumerator.Current
118 get { return ((IEnumerator<TValue>)this).Current; }
121 public TValue Current
123 get { return enumeration.Current.Value; }
128 enumeration = dictionary.GetEnumerator();
130 } // class DictionaryValueEnumerator<TKey, TValue>
133 // DictionaryValueCollection.cs