转载:Dictionary与HashTable的比较

http://www.itgao.com/html/2007-05/45379.html
  阅读教程 加入网摘: 新浪ViVi POCO网摘 365Key 和讯网摘 天极网摘  
 

Dictionary与HashTable的比较

[日期:2007-05-06]来源:www.itgao.com  作者:[字体:大 中 小]

今天在网上看文章,突然看到了有文章用了Dictionary这个东西,恩,第一次看到,不知道什么东西,并且语法和和别的用法不一样,百思不解,于是找来MSDN看了看,看了才知道,原来是实现IColletion接口的东西,恩,这到没什么,但是我发现一个好功能,这个功能就是类似Hashtable的功能,其实也是对应的Key和Value对应的一对,但是Key和Value的对象都是Colletion,并且和Hashtable一样提供了Add方法,在lookup的时候通 过ContainsKey或是ContainsValue来标识是否有值,这个就是对应Hashtable或是Map的look方法了吧,恩,没用过所以 不知道对应的接口是什么。在Dictionary中取值的时候也比hashtable智能了些,是通过TryGetValue(string Key,out Value),返回值是一个bool类型,呵呵,比hashtable好吧,恩,当然类似其他的什么Clear啊Remove啊那当然是要提供的,在使用 的时候我觉得比hashtable功能要强大,而且凭直觉我觉得是比table和map效率要高的,并且代码更安全,所以我觉得这个应该是替代 hashtable和hashmap这两个东西的,呵呵,因为我对hashtable和hashmap认识并不多,什么根本没用过,只是听别人说过,恩, Dictionary的语法初次见比较怪,我把我自己测试的代码发上来。
string strValue = string.Empty; //key
int intValue; //key对应的值
strValue = this.textBox1.Text.Trim();//得到文本框输入的key值

//从Dictionary中取得数据
if (Test(strValue, out intValue) == true)
{
//取得成功的场合
MessageBox.Show(intValue.ToString());
}
else
{
//取得事败的场合
MessageBox.Show("This key has not found!");
}

/// <summary>
/// 从Dictionary中取得数据
/// </summary>
/// <param name="strValue"></param>
/// <param name="intValue"></param>
/// <returns></returns>
private bool Test(string strValue,out int intValue)
{
//声明一个Dictionary对象,key和Value为string和int类型
System.Collections.Generic.Dictionary<string, int> dia = new System.Collections.Generic.Dictionary<string, int>();
//TODO
//测试用数据
dia.Add("jiajunyong", 555);
//返回结果
return dia.TryGetValue(strValue, out intValue);
}



阅读:43
录入:admin

评论 】 【 推荐 】 【 打印 】
上一篇:单登陆实现-同时只能登陆一个
下一篇:Asp.net调用rar.exe压缩的实现
 
  相关教程        
 
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> width="468" scrolling="no" height="15" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9733229352463134&dt=1186640425560&lmt=1186071949&format=468x15_0ads_al_s&output=html&correlator=1186640425560&channel=6322731414&url=http%3A%2F%2Fwww.itgao.com%2Fhtml%2F2007-05%2F45379.html&color_bg=F8FBFE&color_text=000000&color_link=000033&color_url=008000&color_border=F8FBFE&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26q%3Ddictionary%2Bhashtable%2B%25E5%258C%25BA%25E5%2588%25AB%26btnG%3DGoogle%2B%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3Dt%26oq%3Ddictionary%2Bhashtable%2B&cc=100&ga_vid=1216331609.1186640426&ga_sid=1186640426&ga_hid=1776237324&flash=9&u_h=1024&u_w=1280&u_ah=987&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=27&u_nmime=113" name="google_ads_frame">
 
  本文评论       全部评论  
   
  发表评论  
 


点评: 字数
姓名:

 
 
   
width="0" height="0" style="visibility: hidden;" border="0" src="http://www.itgao.com/frm_hit.aspx?id=45379&disp=1">
  教程查询  
 


 
   
  width="250" scrolling="no" height="250" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9733229352463134&dt=1186640426592&lmt=1186071949&prev_fmts=468x15_0ads_al_s&format=250x250_as&output=html&correlator=1186640425560&channel=6322731414&pv_ch=6322731414%2B&url=http%3A%2F%2Fwww.itgao.com%2Fhtml%2F2007-05%2F45379.html&color_bg=E6F4FF&color_text=191919&color_link=000000&color_url=B3B3B3&color_border=E6F4FF&ad_type=text_image&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26q%3Ddictionary%2Bhashtable%2B%25E5%258C%25BA%25E5%2588%25AB%26btnG%3DGoogle%2B%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3Dt%26oq%3Ddictionary%2Bhashtable%2B&cc=100&ga_vid=1216331609.1186640426&ga_sid=1186640426&ga_hid=1776237324&flash=9&u_h=1024&u_w=1280&u_ah=987&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true&u_nplug=27&u_nmime=113" name="google_ads_frame">  
   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值