现在需要使用一个名值对型的对象保存一组二维数组,但是对于System.Collection命名空间中的几个对象小弟全不熟悉。具体的要求是能够根据某一个具体的INT值,将对应的二维数组获取出来,而且希望尽量减少类型转换造成的性能损耗。也就是说,如果建议小弟存在Application或者Session中的话,那还是不浪费您打字的力气的,将获取出来的数据转换为二维数组就会损失不少性能,恐怕BOSS不会同意的。目前小弟想到的是构造一个Dictionary<int,string[,]>对象,不过又头疼如何遍历每个名值对,以及如何根据具体的int值获取到对应的数组。求高手指点。或者能给出更好的建议也行,因为小弟对HashTable什么的一点了解都没有。提前谢过了

解决方案 »

  1.   

    LZ的要求用HashTable就没办法了,因为HashTable的健和值都是存的object,所以取出来的时候还是要转换。
      

  2.   

    用Dictionary <int,string[,]>不用遍历的,取值非常方便,比如:        Dictionary<int, string[]> ff = new Dictionary<int, string[]>();
            ff.Add(1, new string[2] { "1", "2" });
            ff.Add(2, new string[2] { "1", "2" });
            string[] ss = ff[1];
    还有看LZ的技术分不低哟,居然说没用过HashTable,真有点不相信,表忽悠我哟,偶是菜鸟,呵呵
      

  3.   

    还有遍历        Dictionary<int, string[]> ff = new Dictionary<int, string[]>();
            ff.Add(1, new string[2] { "1", "2" });
            ff.Add(2, new string[2] { "1", "2" });
            string[] ss = ff[1];
            Dictionary<int, string[]>.Enumerator enu = ff.GetEnumerator();
            while (enu.MoveNext())
            {
                KeyValuePair<int, string[]> current = enu.Current;
                int key = current.Key;
                string[] value = current.Value;
            }
    偶也不是很熟悉,没有具体测试过,不过应该就是这样的用法,感觉这些函数都差不多,点了之后看着提示找函数用吧,呵呵
      

  4.   

    using System;
    using System.Collections.Generic;class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, string[]> ff = new Dictionary<int, string[]>();
            ff.Add(1, new string[2] { "1", "2" });
            ff.Add(2, new string[2] { "3", "4" });
            int find = 2;
            foreach (KeyValuePair<int, string[]> f in ff)
            {
                if (f.Key == find)
                {
                    foreach (string str in f.Value)
                    {
                        Console.WriteLine(str);
                    }
                }
            }
        }
    }
      

  5.   

    多谢各位支招。小弟目前做的这小段程序就是为了提高响应速度的,所以用了不少缓存技术,因此对于响应时间和性能损耗BOSS一直很重视。HashTable小弟确实没用过,以前构造SQL语句的时候用过一些NameValueCollection而已。小弟试试看吧,结贴给分,辛苦各位了