2013-03-08 30 views
1

看起來我永遠不會遇到問題。現在我想初始化這樣的解釋(正是這一點):我如何創建一個真正複雜的集合?

Dictionary<string, Dictionary<int, int>> origRes = new Dictionary<string, Dictionary<int, int>> 

不然我需要一個集合,它可以包含一個字符串和兩個整數值。並且將足夠簡單的forforeach循環來收集所有這些數據(該字符串可以是一個關鍵值)?

更新

public static Dictionary<int, int> resRequest(string requestedForm) 
     { 
      Dictionary<int, int> kp = new Dictionary<int, int>(); 
      Dictionary<string, KeyValuePair<int, int>> origRes = new Dictionary<string, KeyValuePair<int, int>>(); 
      origRes.Add("Polübiosz", new KeyValuePair<int, int>(560,310)); 
      origRes.Add("Morze", new KeyValuePair<int, int>(690, 510)); 
      origRes.Add("Caesar", new KeyValuePair<int, int>(700, 500)); 
      origRes.Add("OwnCrypt", new KeyValuePair<int, int>(830, 570)); 
      origRes.Add("Hamming", new KeyValuePair<int, int>(850, 500)); 
      origRes.Add("Kezdőkép", new KeyValuePair<int, int>(1280, 1024)); 

      foreach(KeyValuePair<string,KeyValuePair<int, int>> pair in origRes) 
      { 
       if(pair.Key==requestedForm) 
       { 
        kp.Add(pair.Value.Key,pair.Value.Value); 
       } 
      } 
      return kp; 
     } 

這裏是我完整的方法,我想拿到鑰匙,並從KeyValuePair值,但返回的字典不包含它或我不知道,但在調用方沒有鍵或值屬性只有鍵和值。

回答

3

爲什麼不創建一個包含字符串,int,int的類並使用它填充更簡單的字典?

所以不是

Dictionary<string, Dictionary<int, int>> 

務必:

Dictionary<string, MyType> 

如果MyType的是一樣的東西:

class MyType { 
    public string KeyVal { get; set; } // optional - to match your dict key 
    public int Int1Val{ get; set; } 
    public int Int2Val{ get; set; } 
}