实现IDictionary接口

下面是实现了IDictionary接口的类

   public class SerializeDictionary : IDictionary
    {
      
        #region 类成员、方法、构造器
        private DictionaryEntry[] Items;
        private Int32 ItemCount=0;
        public SerializeDictionary(Int32 itemCount)
        {
            this.Items = new DictionaryEntry[itemCount];
        }
        private Boolean TryGetIndexOfKey(Object key, out Int32 index)
        {
            for (index = 0; index < ItemCount; index++)
            {
                if (this.Items[index].Key.Equals(key))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
        #region IDictionary 成员
        public void Add(object key, object value)
        {
            if(Items.Length>ItemCount)
            
            this.Items[ItemCount + 1] = new DictionaryEntry(key, value);
        }
        public void Clear()
        {
            this.ItemCount = 0;
            this.Items = null;
        }
        public bool Contains(object key)
        {
            Int32 index;
            return this.TryGetIndexOfKey(key,out index);
        }
        public IDictionaryEnumerator GetEnumerator()
        {
            return new DictionaryEnumertor(this);
        }
        public bool IsFixedSize
        {
            get { return false; }
        }
        public bool IsReadOnly
        {
            get { return false; }
        }
        public ICollection Keys
        {
            get 
            {
                Object[] Keys = new Object[ItemCount];
                for (Int32 i = 0; i < ItemCount; i++)
                Keys[i] = Items[i].Key;
                return Keys;
            }
        }
        public void Remove(object key)
        {
            Int32 index;
            if (TryGetIndexOfKey(key, out index))
            {
                Array.Copy(this.Items,index+1,Items,index,ItemCount-index-1);
                ItemCount--;
            }
        }
        public ICollection Values
        {
            get 
            {
                Object[] Values = new Object[ItemCount];
                for (int i = 0; i < ItemCount; i++)
                Values[i] = Items[i].Value;
                return Values;
            }
        }
        public object this[object key]
        {
            get
            {
                Int32 index;
               if(TryGetIndexOfKey(key,out index))
               return Items[index].Value;
               return null;
                
            }
            set
            {
               Int32 index;
               if(TryGetIndexOfKey(key, out index))
               this.Items[index].Value = value;
               this.Add(key,value);
            }
        }
        #endregion
        #region ICollection 成员
        public void CopyTo(Array array, int index)
        {
            if (index > array.Length||array==null)
            
            throw new NotImplementedException("index Or array has error!");
            Array.Copy(this.Items, index + 1, array, index, ItemCount - index - 1);
        }
        public int Count
        {
            get { return this.ItemCount; }
        }
        public bool IsSynchronized
        {
            get { return true; }
        }
        public object SyncRoot
        {
            get 
            {
                lock (this) 
                {
                    if (this.Items == null)
                    
                    throw new NotImplementedException("Is Null");
                    return new SerializeDictionary(100);
                }
                
            }
        }
        #endregion
        #region IEnumerable 成员
        /// <summary>
        /// dsaaaaaaaaaaaaaaaaaaaa
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }
        #endregion
        #region 用于迭代的嵌套类
        private class DictionaryEnumertor : IDictionaryEnumerator
        {
            DictionaryEntry[] Items;
            Int32 index = -1;
            public DictionaryEnumertor(SerializeDictionary sd)
            {
                Items = new DictionaryEntry[sd.Count];
                Array.Copy(sd.Items, 0, Items, 0, sd.Count);
            }
            #region IDictionaryEnumerator 成员
            public DictionaryEntry Entry
            {
                get
                {
                    return (DictionaryEntry)Items[index];
                }
            }
            public object Key
            {
                get
                {
                    return Items[index].Key;
                }
            }
            public object Value
            {
                get
                {
                    return Items[index].Value;
                }
            }
            #endregion
            #region IEnumerator 成员
            public object Current
            {
                get
                {
                    return Items[index];
                }
            }
            public bool MoveNext()
            {
                if (index < Items.Length - 1)
                {
                    index++; return true;
                }
                return false;
            }
            public void Reset()
            {
                index = -1;
            }
            #endregion
        }
        #endregion
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值