1
//-----------------------------------------------------------------------
2 // <copyright file="EmptyDictionary.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.Messaging
{
9 using System
.Collections
.Generic
;
13 /// An empty dictionary. Useful for avoiding memory allocations in creating new dictionaries to represent empty ones.
15 /// <typeparam name="TKey">The type of the key.</typeparam>
16 /// <typeparam name="TValue">The type of the value.</typeparam>
17 internal class EmptyDictionary
<TKey
, TValue
> : IDictionary
<TKey
, TValue
> {
19 /// The singleton instance of the empty dictionary.
21 internal static readonly EmptyDictionary
<TKey
, TValue
> Instance
= new EmptyDictionary
<TKey
, TValue
>();
24 /// Prevents a default instance of the EmptyDictionary class from being created.
26 private EmptyDictionary() {
30 /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
34 /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
36 public ICollection
<TValue
> Values
{
37 get { return EmptyList<TValue>.Instance; }
41 /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
45 /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
52 /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
55 /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
57 public bool IsReadOnly
{
62 /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
66 /// An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>.
68 public ICollection
<TKey
> Keys
{
69 get { return EmptyList<TKey>.Instance; }
73 /// Gets or sets the value with the specified key.
75 /// <param name="key">The key being read or written.</param>
76 public TValue
this[TKey key
] {
77 get { throw new KeyNotFoundException(); }
78 set { throw new NotSupportedException(); }
82 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
84 /// <param name="key">The object to use as the key of the element to add.</param>
85 /// <param name="value">The object to use as the value of the element to add.</param>
86 /// <exception cref="T:System.ArgumentNullException">
87 /// <paramref name="key"/> is null.
89 /// <exception cref="T:System.ArgumentException">
90 /// An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
92 /// <exception cref="T:System.NotSupportedException">
93 /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
95 public void Add(TKey key
, TValue
value) {
96 throw new NotSupportedException();
100 /// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key.
102 /// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</param>
104 /// true if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false.
106 /// <exception cref="T:System.ArgumentNullException">
107 /// <paramref name="key"/> is null.
109 public bool ContainsKey(TKey key
) {
114 /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
116 /// <param name="key">The key of the element to remove.</param>
118 /// true if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
120 /// <exception cref="T:System.ArgumentNullException">
121 /// <paramref name="key"/> is null.
123 /// <exception cref="T:System.NotSupportedException">
124 /// The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.
126 public bool Remove(TKey key
) {
131 /// Gets the value associated with the specified key.
133 /// <param name="key">The key whose value to get.</param>
134 /// <param name="value">When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the <paramref name="value"/> parameter. This parameter is passed uninitialized.</param>
136 /// true if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false.
138 /// <exception cref="T:System.ArgumentNullException">
139 /// <paramref name="key"/> is null.
141 public bool TryGetValue(TKey key
, out TValue
value) {
142 value = default(TValue
);
146 #region ICollection<KeyValuePair<TKey,TValue>> Members
149 /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
151 /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
152 /// <exception cref="T:System.NotSupportedException">
153 /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
155 public void Add(KeyValuePair
<TKey
, TValue
> item
) {
156 throw new NotSupportedException();
160 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
162 /// <exception cref="T:System.NotSupportedException">
163 /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
165 public void Clear() {
166 throw new NotSupportedException();
170 /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
172 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
174 /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
176 public bool Contains(KeyValuePair
<TKey
, TValue
> item
) {
181 /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
183 /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
184 /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
185 /// <exception cref="T:System.ArgumentNullException">
186 /// <paramref name="array"/> is null.
188 /// <exception cref="T:System.ArgumentOutOfRangeException">
189 /// <paramref name="arrayIndex"/> is less than 0.
191 /// <exception cref="T:System.ArgumentException">
192 /// <paramref name="array"/> is multidimensional.
194 /// <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
196 /// The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.
198 /// Type <paramref name="T"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.
200 public void CopyTo(KeyValuePair
<TKey
, TValue
>[] array
, int arrayIndex
) {
204 /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
206 /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
208 /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
210 /// <exception cref="T:System.NotSupportedException">
211 /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
213 public bool Remove(KeyValuePair
<TKey
, TValue
> item
) {
219 #region IEnumerable<KeyValuePair<TKey,TValue>> Members
222 /// Returns an enumerator that iterates through the collection.
225 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
227 public IEnumerator
<KeyValuePair
<TKey
, TValue
>> GetEnumerator() {
228 return Enumerable
.Empty
<KeyValuePair
<TKey
, TValue
>>().GetEnumerator();
233 #region IEnumerable Members
236 /// Returns an enumerator that iterates through a collection.
239 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
241 System
.Collections
.IEnumerator System
.Collections
.IEnumerable
.GetEnumerator() {
242 return EmptyEnumerator
.Instance
;