首页 / 软件开发 / C# / 构建可反转排序的泛型字典类(8)--实现IDictionary接口
构建可反转排序的泛型字典类(8)--实现IDictionary接口2010-11-268. 实现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;
}