zoukankan      html  css  js  c++  java
  • C# 键值对类相关

    本文转自博客园:http://www.cnblogs.com/SharkBin/archive/2012/11/02/2750840.html

     

    一 C# 键值对类有以下类:

    ①    IDictionary<string, Object> idc = new Dictionary<string, object>();

    ②    KeyValuePair<string, object> par = (KeyValuePair<string, object>)shoplistcomboBox.SelectedItem;

    ③    Hashtable ht=new Hashtable(); file创建一个Hashtable实例
      
           ht.Add(E,e);添加keyvalue键值对

     Hashtable 内的每一组对象就是一个DictionaryEntry 
     例如我们要循环hashtable 
     foreach (DictionaryEntry de in myHashtable) {...} 
     Hashtable就是一个DictionaryEntry的集合

     

    二 KeyValuePair和Hashtable区别

    KeyValuePair是单个的键值对对象,而Hashtable是一个集合。

    KeyValuePair用于接收combox选定的值。

    例如:KeyValuePair<string, object> par = (KeyValuePair<string, object>)shoplistcomboBox.SelectedItem;

     

     三 hashtable 与 dictionary的区别

    1:单线程程序中推荐使用 Dictionary, 有泛型优势, 且读取速度较快, 容量利用更充分.

    2:多线程程序中推荐使用 Hashtable, 默认的 Hashtable 允许单线程写入, 多线程读取, 对 Hashtable 进一步调用 Synchronized() 方法可以获得完全线程安全的类型. 而 Dictionary 非线程安全, 必须人为使用 lock 语句进行保护, 效率大减.

    3:Dictionary 有按插入顺序排列数据的特性 (注: 但当调用 Remove() 删除过节点后顺序被打乱), 因此在需要体现顺序的情境中使用 Dictionary 能获得一定方便.

  • 相关阅读:
    nginx1.9+新增tcp/udp代理stream
    mysql授权
    搭建GIT服务器
    Centos调出图形化的网络管理
    吾爱破解论坛纪念壁纸(怀念)
    VNC-tigervnc-server远程调用图形化
    Linux生成高强度密码
    Linux学习必备
    git编译
    书单
  • 原文地址:https://www.cnblogs.com/ywei221/p/3630963.html
Copyright © 2011-2022 走看看