using System;using System.Collections.Generic;using System.Collections;using System.Text;namespace fanxing...{ public class SimpleDictionary : IDictionary ...{ private DictionaryEntry[] items; private Int32 ItemsInUse = 0; public SimpleDictionary(Int32 numItems) ...{ items = new DictionaryEntry[numItems]; } public bool IsReadOnly ...{ get ...{ return false; } } public bool Contains(object key) ...{ Int32 index; return TryGetIndexOfKey(key, out index); } public bool IsFixedSize ...{ get ...{ return false; } } public void Remove(object key) ...{ if (key == null) throw new ArgumentNullException("key"); Int32 index; if (TryGetIndexOfKey(key, out index)) ...{ Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1); ItemsInUse--; } else ...{ } } public void Clear() ...{ if(ItemsInUse!=0) ...{ for (int i = 0; i < ItemsInUse;i++ ) ...{ items[i].Key = null; items[9].Value = null; } } ItemsInUse = 0; } public void Add(object key, object value) ...{ try ...{ if (ItemsInUse == items.Length) throw new InvalidOperationException("The dictionary cannot hold any more items."); items[ItemsInUse++] = new DictionaryEntry(key, value); } catch (Exception e) ...{ Console.Write(e.Message); } } public ICollection Keys ...{ get ...{ Object[] keys = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) keys[n] = items[n].Key; return keys; } } public ICollection Values ...{ get ...{ Object[] values = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) values[n] = items[n].Value; return values; } } public object this[object key] ...{ get ...{ Int32 index; if (TryGetIndexOfKey(key, out index)) ...{ return items[index].Value; } else ...{ return null; } } set ...{ Int32 index; if (TryGetIndexOfKey(key, out index)) ...{ items[index].Value = value; } else ...{ Add(key, value); } } } private Boolean TryGetIndexOfKey(Object key, out Int32 index) ...{ for (index = 0; index < ItemsInUse; index++) ...{ if (items[index].Key.Equals(key)) return true; } return false; } private class SimpleDictionaryEnumerator : IDictionaryEnumerator ...{ DictionaryEntry[] items; Int32 index = -1; public SimpleDictionaryEnumerator(SimpleDictionary sd) ...{ items = new DictionaryEntry[sd.Count]; Array.Copy(sd.items, 0, items, 0, sd.Count); } public Object Current ...{ get ...{ ValidateIndex(); return items[index]; } } public DictionaryEntry Entry ...{ get ...{ return (DictionaryEntry)Current; } } public Object Key ...{ get ...{ ValidateIndex(); return items[index].Key; } } public Object Value ...{ get ...{ ValidateIndex(); return items[index].Value; } } public Boolean MoveNext() ...{ if (index < items.Length - 1) ...{ index++; return true; } return false; } private void ValidateIndex() ...{ if (index < 0 || index >= items.Length) throw new InvalidOperationException("Enumerator is before or after the collection."); } public void Reset() ...{ index = -1; } } public IDictionaryEnumerator GetEnumerator() ...{ return new SimpleDictionaryEnumerator(this);//这里需要注意,返回的是一个接口,那我们new的类即SimpleDictionaryEnumerator就必须要继承该接口,从而返回的该对象new SimpleDictionaryEnumerator(this)实例只能用IDictionaryEnumerator 接口下定义的方法。 } public bool IsSynchronized ...{ get ...{ return false; } } public object SyncRoot ...{ get ...{ throw new NotImplementedException(); } } public int Count ...{ get ...{ return ItemsInUse; } } public void CopyTo(Array array, int index) ...{ throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() ...{ return ((IDictionary)this).GetEnumerator(); } class Application7 ...{ static void Main(string[] args) ...{ SimpleDictionary aa = new SimpleDictionary(2); aa.Add("a", "aaa"); aa.Add("b", "aaa"); Console.Write(aa.Keys.Count+"===="+aa.Values.Count); IEnumerable bb = aa; bb.GetEnumerator(); foreach(DictionaryEntry cc in aa) ...{ Console.Write(cc.Key); } Console.Read(); } } }} 这是我抄自msdn的例子:IDictionary接口是hashtable实现的重要接口,对于复杂的hashtable大家可能感觉过于麻烦,东西太多,一头雾水,因为hashtable里面有许多东西实现起来却是麻烦,许多数据结构的知识在里面,容易让人郁闷。但是看了SimpleDictionary 这个类的实现,大家就可以对hashtable的实现有了大体甚至是全局的了解,看了以后会让你茅塞顿开的,里面有些小细节与微软的方法还是有出入,实现一个方法有很多种,只要实现即可。