此文记录的是一个Hashtable的增强版本,以前没泛型的时候笔者喜欢用Hashtable,性能刚刚的。

/***

    Hashtable工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    Date:   2024-01-15 15:18:00

***/

namespace Lzhdim.LPF.Utility
{
    using System.Collections;

    /// <summary>
    /// The Object inherit of Hashtable
    /// </summary>
    public class HashtableExtend : Hashtable
    {
        /// <summary>
        /// 成员变量
        /// </summary>
        private Hashtable hto = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public HashtableExtend()
        {
            this.hto = new Hashtable();
        }

        /// <summary>
        /// Add New item
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public override void Add(object key, object value)
        {
            this.hto.Add(key, value);
        }

        /// <summary>
        /// Create a copy Hashtable of the Object
        /// </summary>
        /// <returns></returns>
        public Hashtable Copy()
        {
            Hashtable ht = new Hashtable();

            foreach (DictionaryEntry de in this.hto)
            {
                ht.Add(de.Key, de.Value);
            }

            return ht;
        }

        /// <summary>
        /// Remove a item by key
        /// </summary>
        /// <param name="key"></param>
        public override void Remove(object key)
        {
            this.hto.Remove(key);
        }

        /// <summary>
        /// ASC Sort by key
        /// </summary>
        public void SortByKeyASC()
        {
            ArrayList arrList = new ArrayList(this.hto.Keys);
            arrList.Sort();

            Hashtable ht = new Hashtable();

            for (int i = 0; i < arrList.Count; i++)
            {
                ht.Add(arrList[i], this.hto[arrList[i]]);
            }

            this.hto.Clear();
            this.hto = ht;
        }

        /// <summary>
        /// DESC Sort by key
        /// </summary>
        public void SortByKeyDESC()
        {
            ArrayList arrList = new ArrayList(this.hto.Keys);
            arrList.Sort();

            Hashtable ht = new Hashtable();

            for (int i = arrList.Count - 1; i >= 0; i++)
            {
                ht.Add(arrList[i], this.hto[arrList[i]]);
            }

            this.hto.Clear();
            this.hto = ht;
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.