2010-10-13 29 views
1

例如,我有一個名爲Temp的類,然後我使用IList<Temp>爲它分配值。 填充IList<Temp>後,我創建了一個字典併爲每個對象分配一個int鍵。 我的問題是,如何從IList中刪除名稱爲b的temp_value以及鍵值爲「2」的字典?如何使用字典鍵刪除IList <Class>值

class Temp 
{ 
    public string name { get; set; } 

    public Temp(string n) 
    { 
     this.name = n; 
    } 
} 

主要

class Program 
{ 
    static void Main(string[] args) 
    { 
     IList<Temp> temp_value = new List<Temp>(); 

     temp_value.Add(new Temp("a") 
     { 
      name = "a", 
     }); 

     temp_value.Add(new Temp("b") 
     { 
      name = "b", 
     }); 

     temp_value.Add(new Temp("b") 
     { 
      name = "b", 
     }); 

     Dictionary<int, Temp> dic = new Dictionary<int, Temp>(); 

     for (int i = 0; i < temp_value.Count; i++) 
     { 
      dic.Add(i, temp_value[i]);     
     } 
    } 
} 
+0

這是問題更多關於保持的IList和IDictionary中同步,或者簡單地從一個IDictionary移除項目? – spender 2010-10-13 01:07:35

+0

我還希望名稱值爲b的對象在從字典中刪除時被刪除。 – Rye 2010-10-13 01:09:49

回答

1

Dict.remove(2)

這是自從你加入字典中的項目在相同的索引列表中的最簡單的方法

所以如果你使用dict.remove(2)比你能使用IList.remove(2)

+0

謝謝!忘記了'RemoveAt()' – Rye 2010-10-13 01:22:17