Nullable: System.Runtime.InteropServices.CustomMarshalers/WindowsRuntime (#23930)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / src / System / Runtime / InteropServices / WindowsRuntime / DictionaryKeyCollection.cs
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.
4
5 #nullable enable
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Diagnostics;
9
10 namespace System.Runtime.InteropServices.WindowsRuntime
11 {
12     [DebuggerDisplay("Count = {Count}")]
13     internal sealed class DictionaryKeyCollection<TKey, TValue> : ICollection<TKey>
14     {
15         private readonly IDictionary<TKey, TValue> dictionary;
16
17         public DictionaryKeyCollection(IDictionary<TKey, TValue> dictionary)
18         {
19             if (dictionary == null)
20                 throw new ArgumentNullException(nameof(dictionary));
21
22             this.dictionary = dictionary;
23         }
24
25         public void CopyTo(TKey[] array, int index)
26         {
27             if (array == null)
28                 throw new ArgumentNullException(nameof(array));
29             if (index < 0)
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);
35
36             int i = index;
37             foreach (KeyValuePair<TKey, TValue> mapping in dictionary)
38             {
39                 array[i++] = mapping.Key;
40             }
41         }
42
43         public int Count
44         {
45             get { return dictionary.Count; }
46         }
47
48         bool ICollection<TKey>.IsReadOnly
49         {
50             get { return true; }
51         }
52
53         void ICollection<TKey>.Add(TKey item)
54         {
55             throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
56         }
57
58         void ICollection<TKey>.Clear()
59         {
60             throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
61         }
62
63         public bool Contains(TKey item)
64         {
65             return dictionary.ContainsKey(item);
66         }
67
68         bool ICollection<TKey>.Remove(TKey item)
69         {
70             throw new NotSupportedException(SR.NotSupported_KeyCollectionSet);
71         }
72
73         IEnumerator IEnumerable.GetEnumerator()
74         {
75             return ((IEnumerable<TKey>)this).GetEnumerator();
76         }
77
78         public IEnumerator<TKey> GetEnumerator()
79         {
80             return new DictionaryKeyEnumerator<TKey, TValue>(dictionary);
81         }
82     }  // public class DictionaryKeyCollection<TKey, TValue>
83
84
85     internal sealed class DictionaryKeyEnumerator<TKey, TValue> : IEnumerator<TKey>
86     {
87         private readonly IDictionary<TKey, TValue> dictionary;
88         private IEnumerator<KeyValuePair<TKey, TValue>> enumeration;
89
90         public DictionaryKeyEnumerator(IDictionary<TKey, TValue> dictionary)
91         {
92             if (dictionary == null)
93                 throw new ArgumentNullException(nameof(dictionary));
94
95             this.dictionary = dictionary;
96             enumeration = dictionary.GetEnumerator();
97         }
98
99         void IDisposable.Dispose()
100         {
101             enumeration.Dispose();
102         }
103
104         public bool MoveNext()
105         {
106             return enumeration.MoveNext();
107         }
108
109         object? IEnumerator.Current
110         {
111             get { return ((IEnumerator<TKey>)this).Current; }
112         }
113
114         public TKey Current
115         {
116             get { return enumeration.Current.Key; }
117         }
118
119         public void Reset()
120         {
121             enumeration = dictionary.GetEnumerator();
122         }
123     }  // class DictionaryKeyEnumerator<TKey, TValue>
124 }
125
126 // DictionaryKeyCollection.cs