Hashtable工具类 - C#小函数类推荐

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

/***

    Hashtable工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    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;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzhdim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值