# 新手求助关于Dictionary<KeyValuePair<int, int>, string>的问题

0

0

Dictionary<KeyValuePair<int, int>, string> d = new Dictionary<KeyValuePair<int, int>, string>();

for(int i=0;i<100;i++)

for(int j=0;j<100;j++)

，请你赐教下比较灵活的取值方法，谢谢

@梦天涯: 你可以根据具体需求写个扩展方法！

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<KeyValuePair<int, int>, string> d = new Dictionary<KeyValuePair<int, int>, string>();
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)

string s = d.GetVal(2, 3);
Console.WriteLine(s);

}
}

public static class DictionaryExtension
{
public static string GetVal(this Dictionary<KeyValuePair<int, int>, string> d, int key, int value)
{
return d[new KeyValuePair<int, int>(key, value)];
}
}
}```

0

0

Dictionary<KeyValuePair<int, int>, string> dict = new Dictionary<KeyValuePair<int, int>, string>();

dict[new KeyValuePair<int, int>(0, 0)] = "aaa";

dict[new KeyValuePair<int, int>(99, 99)] = "aaa";

weizhangxiaohan | 园豆：95 (初学一级) | 2016-08-31 14:57
0
``` 1  Dictionary<KeyValuePair<int,int>,string> dic = new Dictionary<KeyValuePair<int, int>, string>();
2
3
5
6             dic.Add(new KeyValuePair<int, int>(2, 2), "2");
7
8             foreach (var item in dic)
9             {
10                 Console.WriteLine(string.Format("keykey:{0},keyvalue:{1},value:{2}", item.Key.Key, item.Key.Value, item.Value));
11             }
12
13             Console.WriteLine(dic[new KeyValuePair<int, int>(1,1)]);
14