Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Last active July 7, 2016 05:29
Show Gist options
  • Save JeffreyZhao/6397890716cf3c96338595d0bd5666a1 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/6397890716cf3c96338595d0bd5666a1 to your computer and use it in GitHub Desktop.
namespace HashTableDict
{
using System.Collections;
using System.Collections.Generic;
public class Hashtable<TKey, TValue> : IDictionary<TKey, TValue>
where TKey : class
where TValue : class
{
private readonly Hashtable _ht = new Hashtable();
// 实现以下接口。
public int Count
{
get { throw new System.NotImplementedException(); }
}
public bool ContainsKey(TKey key)
{
throw new System.NotImplementedException();
}
public void Add(TKey key, TValue value)
{
throw new System.NotImplementedException();
}
public bool Remove(TKey key)
{
throw new System.NotImplementedException();
}
public bool TryGetValue(TKey key, out TValue value)
{
throw new System.NotImplementedException();
}
public TValue this[TKey key]
{
get { throw new System.NotImplementedException(); }
set { throw new System.NotImplementedException(); }
}
public void Clear()
{
throw new System.NotImplementedException();
}
/* 以下接口无需实现 */
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
throw new System.NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new System.NotImplementedException();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
throw new System.NotImplementedException();
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new System.NotImplementedException();
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new System.NotImplementedException();
}
public bool IsReadOnly
{
get { throw new System.NotImplementedException(); }
}
public ICollection<TKey> Keys
{
get { throw new System.NotImplementedException(); }
}
public ICollection<TValue> Values
{
get { throw new System.NotImplementedException(); }
}
}
}
@DevinDong
Copy link

DevinDong commented Jul 7, 2016

    // 实现以下接口。

    public int Count
    {
        get { return _ht.Count; }
    }

    public bool ContainsKey(TKey key)
    {
        return _ht.ContainsKey(key);
    }

    public void Add(TKey key, TValue value)
    {
        _ht.Add(key, value);
    }

    public bool Remove(TKey key)
    {
        if (key == null)
        {
            throw new ArgumentNullException();
        }
        if (_ht.ContainsKey(key))
        {
            _ht.Remove(key);
            return true;
        }
        return false;
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
        try
        {
            if (_ht.ContainsKey(key))
            {
                value = (TValue)_ht[key];
                return true;
            }
        }
        catch (Exception e)
        {
            //do something here...
        }
        value = null;
        return false;
    }

    public TValue this[TKey key]
    {
        get { return (TValue)_ht[key]; }
        set { _ht[key] = value; }
    }

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment