首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

C#中Hashtable Dictionary的使用方法探索

    using System;
    using System.Collections;

    namespace testSortedList
    {
        class Class1
         {
             [STAThread]
        static void Main(string[] args)
         {
             SortedList sl = new SortedList(new MySort());        //不排序 [Page]
             sl.Add(333,333);
             sl.Add(111,111);
             sl.Add(222,222);
             sl.Add(111,112);

             PrintList(sl);

             Console.ReadLine();
         }

        private static void PrintList(SortedList sl)
         {
            for(int i=0;i<sl.Count ;i++)
             {
                 Console.WriteLine(\"{0}\\t{1}\",sl.GetKey(i),sl.GetByIndex(i));
             }//end for
         }//end fn()

     }
    public class MySort:IComparer
     {
        #region IComparer 成员
        public int Compare(object x, object y)
         {
            return -1;

            //排序
            int iResult = (int)x - (int)y;


          if(iResult == 0) iResult = -1;
           return iResult;

         }
        #endregion
     }

    }


    使用单链接列表实现 IDictionary。建议用于通常包含 10 个或 10 个以下项的集合。
    最后我测试了使用泛类型的Dictionary<T,T>, 尽管msdn上说hashtable和Dictionary的实现是一样的,不过同样的数据,返回的结果却是不同的,我没有找到更多的解释,测试代码如下

   using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    namespace NoSortHashtable
    {
     /// <summary>
     /// Summary description for Class1.
     /// </summary>
     public class Class1
     {
         /// <summary>
         /// The main entry point for the application.
         /// </summary>
         [STAThread]
         static void Main(string[] args)
         {
             Hashtable ht = new Hashtable();
             ht.Add(\"hunan\",\"changsha\");
             ht.Add(\"beijing\",\"beijing\");
             ht.Add(\"anhui\",\"hefei\");
             ht.Add(\"sichuan\",\"chengdu\");
             foreach(string str in ht.Keys)
             {
                 Console.WriteLine(str + \" : \" + ht[str]);
             }

             Console.WriteLine(\"------------------------------------\");

             Dictionary<String,String> dic = new Dictionary<String,String>();
             dic.Add(\"hunan\",\"changsha\"); [Page]
             dic.Add(\"beijing\",\"beijing\");
             dic.Add(\"anhui\",\"hefei\");
             dic.Add(\"sichuan\",\"chengdu\");
             foreach(string str in dic.Keys)
             {
                 Console.WriteLine(str + \" : \" + dic[str]);
             }

             Console.WriteLine(\"------------------------------------\");

             ListDictionary lsdic = new ListDictionary();
             lsdic.Add(\"hunan\",\"changsha\");
             lsdic.Add(\"beijing\",\"beijing\");
             lsdic.Add(\"anhui\",\"hefei\");
             lsdic.Add(\"sichuan\",\"chengdu\");
            
             foreach(string str in lsdic.Keys)
             {
                 Console.WriteLine(str + \" : \" + lsdic[str]);
                 }
            }
       }
    }

上一页  [1] [2] 

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航