想要创建Dictionary <int,t =“”> </int,>类型的自定义类

时间:2009-06-08 00:00:40

标签: c# generics dictionary

我想创建一个基本上包装字典的自定义类。

我想为它添加一个名为Name的属性。

我试过了:

public class MyDictionary<int, T> : Dictionary<int, T>
{
        public string Name { get; set;}

}

似乎没有用,有什么想法吗?

更新

我得到的错误是:

Type parameter declaration must be an identifier not a type 

4 个答案:

答案 0 :(得分:33)

问题在于您的声明。您的自定义类只需要一个类型参数,因为int类型永远不会变化:

public class MyDictionary<T> : Dictionary<int, T>
{
    public string Name { get; set; }
}

答案 1 :(得分:0)

如果你可以包装字典,那么为什么不呢:

public class Context<T>
{
    private Dictionary<int, T> dictContext;
    public String Name { get; private set; }

    public Context (String name)
    {
        this.Name = name;
        dictContext = new Dictionary<int, T>();
    }

您可以添加所有词典成员:

    public int Count { 
        get {  return dictContext.Count();  }
    }
    public Dictionary<int, T>.KeyCollection Keys
    {
        get { return dictContext.Keys; }
    }
    public Dictionary<int, T>.ValueCollection Values
    {
        get { return dictContext.Values; }
    }


    public void Add (int key, T value)
    {
        dictContext.Add(key, value);
    }

    public bool Remove (int key)
    {
       return  dictContext.Remove(key);
    }

并声明自己的

    public void MyFunction ()
    {
        //do nothing
    }
}

答案 2 :(得分:0)

问题出在MyDictionary<int, T>上-您在此处使用类型(int)而不是标识符,因此出现错误Type parameter declaration must be an identifier not a type

如果您使用TKey之类的标识符,它将可以正常工作:

public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
    public string Name { get; set; }
}

,或者如果您希望它是仅以int作为键的字典:

public class MyDictionary<TValue> : Dictionary<int, TValue>
{
    public string Name { get; set; }
}

答案 3 :(得分:0)

我们可以创建自定义词典类:

 public class Dict<TKey, UValue> : IEnumerable<KeyValuePair<TKey, UValue>>
    {
        private LinkedList<KeyValuePair<TKey, UValue>>[]  _values;
        private int capacity;
        public Dict()
        {
            _values = new LinkedList<KeyValuePair<TKey, UValue>>[15];
        }
        public int Count => _values.Length;

        public void Add(TKey key,UValue val)
        {
            var hash = GetHashValue(key);
            if(_values[hash]==null)
            {
                _values[hash] = new LinkedList<KeyValuePair<TKey, UValue>>();
            }
            var keyPresent = _values[hash].Any(p => p.Key.Equals(key));
            if(keyPresent)
            {
                throw new Exception("Duplicate key has been found");
            }
            var newValue = new KeyValuePair<TKey, UValue>(key, val);
            _values[hash].AddLast(newValue);
            capacity++;
            if(Count<= capacity)
            {
                ResizeCollection();
            }
        }

        private void ResizeCollection()
        {
            throw new NotImplementedException();
        }

        public bool ContainsKey(TKey key)
        {
            var hash = GetHashValue(key);
            return _values[hash] == null ? false : _values[hash].Any(p => 
    p.Key.Equals(key));
        }

        public UValue GetValue(TKey key)
        {
            var hash = GetHashValue(key);
            return _values[hash] == null ? default(UValue) :
                _values[hash].First(m => m.Key.Equals(key)).Value;
        }
        public IEnumerator<KeyValuePair<TKey, UValue>> GetEnumerator()
        {
            return (from collections in _values
                    where collections != null
                    from item in collections
                    select item).GetEnumerator();
        }

        private int GetHashValue(TKey key)
        {
            return (Math.Abs(key.GetHashCode())) % _values.Length;
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public UValue this[TKey key]
        {
            get
            {
                int h = GetHashValue(key);
                if (_values[h] == null) throw new KeyNotFoundException("Keys not found");
                return _values[h].FirstOrDefault(p=>p.Key.Equals(key)).Value;
            }
            set
            {
                int h = GetHashValue(key);
                _values[h] = new LinkedList<KeyValuePair<TKey, UValue>>();
                _values[h].AddLast(new KeyValuePair<TKey, UValue>
                                                    (key,value));
            }
        }
    }

static void Main(string[] args)
        {
            Dict<int, int> dict=new Dict<int, int>();
            dict.Add(-1, -1);
            dict.Add(1, -1);
            dict.Add(9, -1);
            dict.ContainsKey(9);
            dict.GetValue(9);
            dict[10] = 34;
            foreach(var item in dict)
            {`enter code here`
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
            Console.WriteLine(dict[10]);
        }