比如Dictionary<char,int> test;
我希望能够char和int互为key,
可以通过char快速检索到对应的int,也可以通过int快速检索到对应char
不知道大家是否明白我的意思?我要规避数组的IndexOf(效率低)
循环test检索也一样,耗时太长了

解决方案 »

  1.   

    比如(a,0),(b,1),(c,2),(d,3)
    我需要根据c快速检索到2,反过来,根据3能快速检索到d
      

  2.   

    Dictionary<char, int> test;
    var charkey = test.Where(t => t.Value == 2).First().Key;
               
      

  3.   

    有没有互为key的不太清楚,不过要达到你这个目的,首先你的key/value的映射关系必须是可逆的
      

  4.   

    net2.0,没有linq语法有没有其他类型能符合我的要求?
      

  5.   

    我发现字典类型的Key检索很快,而数组的IndexOf相比之下简直慢到爆
      

  6.   

    try
    class dictionary
        {
            Dictionary<char, int> test1 = new Dictionary<char, int>();
            Dictionary<int, char> test2 = new Dictionary<int, char>();
    public void Remove(……)
    {……}
            public void Add(char key, int value)
            {
                if (test1.ContainsKey(key) || test2.ContainsKey(value))
                    return;
                test1.Add(key, value);
                test2.Add(value, key);
            }
            public char? this[int index]
            {
                get { return test2.ContainsKey(index) ? (char?)test2[index] : null; }
            }
            public int? this[char index]
            {
                get { return test1.ContainsKey(index) ? (int?)test2[index] : null; }
            }
        }
      

  7.   

    如果我建一个新类
    class A{int pos,char chr}然后List<A>检索,快吗?
      

  8.   


    纠正一下,字典的Key不是用来检索的,而是一个索引,根据这个Key可以直接Hash计算出他存储的值在哪个位置。如果想实现你要的功能,简单,用两个字典