2009-06-08 75 views
2

我想創建一個基本上包裝字典的自定義類。想要創建一個類型的自定義類字典<int, T>

我想向它添加一個名爲Name的屬性。

我想:

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

} 

似乎並不奏效,任何想法?

更新

錯誤我得到的是:

Type parameter declaration must be an identifier not a type 
+0

什麼,具體來說,不起作用? – 2009-06-08 00:02:30

+0

你怎麼使用它? – 2009-06-08 00:05:19

回答

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

這會這樣,我想。

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 
    } 
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 
    } 
}