views:

401

answers:

3

Hi,

I want to create a custom class that basically wraps a dictionary.

I want to add a property to it called Name.

I tried:

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

}

Doesn't seem to be working, any ideas?

Update

THe error I'm getting is:

Type parameter declaration must be an identifier not a type
+18  A: 
public class MyDictionary<T> : Dictionary<int, T>
{
    public string Name { get; set; }
}
John Saunders
@downvoter: what is the problem with this answer?
John Saunders
+1  A: 

Your type declarations need to be generic:

public class MyDictionary<K, V> : Dictionary<K, V>
{
        public string Name { get; set;}

}
Stuart Dunkeld
A: 

This'll do I think.

    public class IntDict<T> : IDictionary<int, T>
    {
        private Dictionary<int,T> internalDict = new Dictionary<int,T>();

        //Sorry forgot this :P
        public String Name { get; set; }

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

        public bool ContainsKey(int key)
        {
            return internalDict.ContainsKey(key);
        }

        public ICollection<int> Keys
        {
            get { return internalDict.Keys; }
        }

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

        public bool TryGetValue(int key, out T value)
        {
            return internalDict.TryGetValue(key, out value);
        }

        public ICollection<T> Values
        {
            get { return internalDict.Values; }
        }

        public T this[int key]
        {
            get
            {
                return internalDict[key];
            }
            set
            {
                internalDict[key] = value;
            }
        }    


        #region ICollection<KeyValuePair<int,T>> Members

        public void Add(KeyValuePair<int, T> item)
        {
            internalDict.Add(item.Key, item.Value);
        }

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

        public bool Contains(KeyValuePair<int, T> item)
        {
            return (internalDict.ContainsKey(item.Key) && internalDict.ContainsValue(item.Value));
        }

        private void CopyTo(KeyValuePair<int, T>[] array, int arrayIndex)
        {
            //Could be done but you prolly could figure this out yourself;
            throw new Exception("do not use");
        }

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

        private bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<int, T> item)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<KeyValuePair<int,T>> Members

        public IEnumerator<KeyValuePair<int, T>> GetEnumerator()
        {    
            return internalDict.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return internalDict.GetEnumerator();
        }

        #endregion
    }
the_ajp