Create a Hashtable Silverlight

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SFire.Framework
{
    /// <summary>
    /// Hash table using Silverlight
    /// Creator: sucsy
    /// Date: 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 Hashi table
    /// <summary>
    /// Hashi table
    /// </summary>
    public class HashTableItem
    {
        /// <summary>
        /// Keyword
        /// </summary>
        public object Key { get; set; }
        /// <summary>
        /// The source object
        /// </summary>
        public object Value { get; set; }
    }
    #endregion
}

Nanjing cool software

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Bernie at November 13, 2013 - 4:35 PM