8. 實現IDictionary<TKey, TValue>接口
由於前面實現了 IDictionary接口,現在實現IDictionary<TKey, TValue>也就沒什麼困難 的了,照葫蘆畫瓢。
首先改變類聲明:
public class ReversibleSortedList<TKey, TValue> :IDictionary<TKey, TValue>
IDictionary, IEnumerable<KeyValuePair<TKey, TValue>>, ICollection,
IEnumerable, ICollection<KeyValuePair<TKey, TValue>>
然後 實現ICollection<KeyValuePair<TKey, TValue>>接口成員:
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get
{
return false;
}
}
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair)
{
this.Add(keyValuePair.Key, keyValuePair.Value);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey (keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
keyValuePair.Value))
{
return true;
}
return false;
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (
KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException ("array");
}
if ((arrayIndex < 0) || (arrayIndex > array.Length))
{
throw new ArgumentOutOfRangeException(
"arrayIndex", "Need a non-negative number");
}
if ((array.Length - arrayIndex) < this.Count)
{
throw new ArgumentException("ArrayPlusOffTooSmall");
}
for (int num1 = 0; num1 < this.Count; num1++)
{
KeyValuePair<TKey, TValue> pair1;
pair1 = new KeyValuePair<TKey, TValue>(
this.keys[num1], this.values[num1]);
array[arrayIndex + num1] = pair1;
}
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
this.values[num1], keyValuePair.Value))
{
this.RemoveAt(num1);
return true;
}
return false;
}