zoukankan      html  css  js  c++  java
  • 定制Dictionary

    在日常开发中,经常会碰到对Dictionary的大量操作,判断等。当然可以用Dictionary本省,也可以重写一个符合自己逻辑处理的Dictionary.

    public class MyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
        {
            IList<TKey> keys = new List<TKey>();
            IList<TValue> values = new List<TValue>();
    
            public MyDictionary() 
            {
            }
    
            public MyDictionary(IDictionary<TKey, TValue> dictionary)
            {
                foreach (var key in dictionary.Keys)
                {
                    this[(TKey)key] = (TValue)dictionary[key];
                }
            }
    
            public ICollection<TKey> keySet()
            {
                return this.Keys;
            }
    
            public bool IsEmpty()
            {
                return (this.Count == 0);
            }
    
            #region MyDictionary<TKey,TValue> Members
    
            public void Add(TKey key, TValue value)
            {
                keys.Add(key);
                values.Add(value);
            }
    
            public bool ContainsKey(TKey key)
            {
                return keys.Contains(key);
            }
    
            public ICollection<TKey> Keys
            {
                get { return new ReadOnlyCollection<TKey>(keys); }
            }
    
            public bool Remove(TKey key)
            {
                int removeLocation = keys.IndexOf(key);
                if (removeLocation < 0)
                    return false;
    
                values.RemoveAt(removeLocation);
                keys.RemoveAt(removeLocation);
                return true;
            }
    
            public bool TryGetValue(TKey key, out TValue value)
            {
                if (keys.Contains(key))
                {
                    value = values[keys.IndexOf(key)];
                    return true;
                }
                value = default(TValue);
                return false;
            }
    
            public ICollection<TValue> Values
            {
                get { return new ReadOnlyCollection<TValue>(values); }
            }
    
            public TValue this[TKey key]
            {
                get
                {
                    if (values.Count <= 0)
                        return default(TValue);
                    else
                    {
                        int nIndex = keys.IndexOf(key);
                        if (nIndex < 0)
                            return default(TValue);
                        else
                            return values[nIndex];
                    }
                }
                set
                {
                    if (keys.Contains(key))
                        values[keys.IndexOf(key)] = value;
                    else
                        Add(key, value);
                }
            }
    
            #endregion
    
            #region ICollection<KeyValuePair<TKey,TValue>> Members
    
            public void Add(KeyValuePair<TKey, TValue> item)
            {
                Add(item.Key, item.Value);
            }
    
            public void Clear()
            {
                keys.Clear();
                values.Clear();
            }
    
            public bool Contains(KeyValuePair<TKey, TValue> item)
            {
                if (keys.Contains(item.Key))
                    return (this[item.Key].Equals(item.Value));
                return false;
            }
    
            public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
            {
                if (array == null)
                {
                    throw new ArgumentNullException("array was null");
                }
                if ((arrayIndex < 0) || (arrayIndex > array.Length))
                {
                    throw new ArgumentOutOfRangeException("arrayIndex");
                }
                if ((array.Length - arrayIndex) < this.Count)
                {
                    throw new ArgumentException("Array plus offset is too small");
                }
    
                foreach (KeyValuePair<TKey, TValue> keyValue in this)
                {
                    array[arrayIndex++] = keyValue;
                }
    
            }
    
            public int Count
            {
                get { return keys.Count; }
            }
    
            public bool IsReadOnly
            {
                get { return false; }
            }
    
            public bool Remove(KeyValuePair<TKey, TValue> item)
            {
                if (keys.Contains(item.Key) && values[keys.IndexOf(item.Key)].Equals(item.Value))
                {
                    Remove(item.Key);
                    return true;
                }
                return false;
            }
    
            #endregion
    
            #region IEnumerable<KeyValuePair<TKey,TValue>> Members
    
            public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
            {
                int index = 0;
                foreach (TKey key in keys)
                    yield return new KeyValuePair<TKey, TValue>(key, values[index++]);
            }
    
            #endregion
    
            #region IEnumerable Members
    
            IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this.Keys.GetEnumerator();
            }
    
            #endregion
        }
    
  • 相关阅读:
    oracle中函数和存储过程的区别和联系
    oracle系统函数(日期函数)
    触发器
    初次使用集合
    框架结构中同时改变多个框架内容,并显示两个页面
    借贷记账法的原理
    IIS7对json支持
    SqlHelper和数据访问层
    在easyui datagrid中formatter数据后使用linkbutton
    jQuery 添加/移除CSS类
  • 原文地址:https://www.cnblogs.com/leon032/p/2287271.html
Copyright © 2011-2022 走看看