Silverlight 下创建Hashtable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SFire.Framework
{
    /// <summary>
    /// Silverlight下使用的哈希表
    /// 创建者:sucsy
    /// 创建日期:2012-2-27
    /// </summary>
    public class DataHashtable : IDictionary
    {
        List<HashTableItem> table = null;
        public DataHashtable()
        {
            this.table = new List<HashTableItem>();
        }

        public void Add(object key, object value)
        {
            if (this.Contains(key) == false)
            {
                table.Add
                    (
                        new HashTableItem()
                        {
                            Key = key,
                            Value = value
                        }
                    );
            }
            else
            {
                this[key] = value;
            }
        }

        public void Clear()
        {
            table.Clear();
        }

        public bool Contains(object key)
        {
            foreach (HashTableItem item in this.table)
            {
                if (item.Key!=null && item.Key.Equals(key)) return true;
            }

            return false;
        }

        public IDictionaryEnumerator GetEnumerator()
        {
            return (IDictionaryEnumerator)this.table.ToArray().GetEnumerator();
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public ICollection Keys
        {
            get { return this.table.Select(item => item.Key).ToArray(); }
        }

        public void Remove(object key)
        {
            HashTableItem item = Find(key);
            if (item != null) this.table.Remove(item);
        }

        private HashTableItem Find(object key)
        {
            HashTableItem find = null;
            foreach (HashTableItem item in this.table)
            {
                if (item.Key.Equals(key))
                {
                    find = item;
                    break;
                }
            }
            return find;
        }

        public ICollection Values
        {
            get { return this.table.Select(item => item.Value).ToArray(); }
        }

        public object this[object key]
        {
            get
            {
                HashTableItem item = Find(key);
                if (item != null) return item.Value;
                return null;
            }
            set
            {
                HashTableItem item = Find(key);
                if (item != null) item.Value = value;
            }
        }

        public void CopyTo(Array array, int index)
        {
            this.table.CopyTo((HashTableItem[])array, index);
        }

        public int Count
        {
            get { return this.table.Count; }
        }

        public bool IsSynchronized
        {
            get { return true; }
        }

        public object SyncRoot
        {
            get { return null; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.table.GetEnumerator();
        }
    }

    #region 哈希表项
    /// <summary>
    /// 哈希表项
    /// </summary>
    public class HashTableItem
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public object Key { get; set; }
        /// <summary>
        /// 源对象
        /// </summary>
        public object Value { get; set; }
    }
    #endregion
}

南京酷得软件


转载自:http://www.cnblogs.com/sucsy/archive/2013/05/25.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值