上一题下一题
跳转到
 
 
  世界如此多姿,发展如此迅速,窥一斑未必还能知全豹。但正如万花筒一样,每一个管窥都色彩斑斓。  
 
 
  知识通道 | 学习首页 | 教师首页 | PK首页 | 知识创造首页 | 企业首页 | 登录
 
本文对应知识领域
C# TryGetValue Method
作者:未知 申领版权
2010年10月13日 共有 2434 次访问 【添加到收藏夹】 【我要附加题目
受欢迎度:

    You want to improve your Dictionary usage by avoiding a common mistake. One example is incrementing the value of a Dictionary at a key. Code that uses ContainsKey for this is doing more than you realize, and the TryGetValue method offers an excellent solution. Here we see examples of using the TryGetValue method in the C# programming language.

    
Avoid this common Dictionary mistake.
    Skip duplicate hash code computations and lookups.

Using TryGetValue method

First, we look at how you can rewrite programs that use ContainsKey with TryGetValue. Even developers who understand Dictionary and how hashtable lookups work can make this mistake. It also gives us an interesting insight into how hashtables in C# work. We apply the TryGetValue method in the C# language.
    === Code that uses ContainsKey (C#) ===using System.Collections.Generic;class Program{ static void Main() { var d = new Dictionary<string, int>(); d.Add("key", 0); // // A. // Does three lookups in Dictionary. // if (d.ContainsKey("key")) { d["key"] ; } }}=== Code that shows what ContainsKey does (C#) ===using System.Collections.Generic;class Program{ static void Main() { var d = new Dictionary<string, int>(); d.Add("key", 0); // // B. // This code is equivalent to A. // if (d.ContainsKey("key")) { d["key"] = d["key"] 1; } }}=== Code that uses TryGetValue (C#) ===using System.Collections.Generic;class Program{ static void Main() { var d = new Dictionary<string, int>(); d.Add("key", 0); // // C. // This code does two hash lookups. // int value; if (d.TryGetValue("key", out value)) { d["key"] = value 1; } }}
    Description of first example. The problematic code increments or decrements the value of a Dictionary at a key. Skilled developers usually increment using the operator. However, on a Dictionary this is a bad choice.
    Second example. The increment does two lookups when you really only need one in the common case above. To increment the value of the Dictionary, you have to get the current value.
    See Dictionary Lookup Comparison.
    Third example with TryGetValue. Every lookup in a hash on a string key has to compute the hash code, which has a performance penalty. To solve this inefficiency, use the TryGetValue method. You can store the value it finds.

    

Benchmark

Here we look at how the TryGetValue method performs when used in the way in the above example. I am so used to using the operator (or --) on integers, that this slipped through my review. Expert developers understand precisely what their code is doing. So knowing this is much more important than simply making the code twice as fast.
    === Benchmark results for TryGetValue (C#) ===Use ContainsKey: 1700 msUse TryGetValue: 1108 ms [faster]

    

Summary

Here we looked at how you can rewrite your Dictionary code to use TryGetValue instead of ContainsKey. Don't use the or -- operators on Dictionary. Instead, store the value with TryGetValue. Careful and precise understanding of how hashtable lookups work in .NET and C# helps improve your code significantly.

    

 

相关新闻

《VB程序设计基础》选择题
如何在Java中避免equals方法的隐藏陷阱
汇编语言程序设计-顺序结构程序设计
第10章 应用程序的设计
实验三 简单的C程序设计
经典C语言程序设计100例71-80
全屏抗锯齿
Notes on Programming in C
并行思维3
并行思维2

您可能对这些感兴趣  

用VB做定时断线程序
VisualBasic中的界面设计原则和编程技巧
VB6.0与Windows API 间的呼叫技巧
制作可以自动隐藏的弹出式菜单
ListBox中的字符串超长显示的解决方法
VB中的Unicode 和 Ansi 格式
优化程序显示速度
Visual Basic 产生渐层的 Form 背景
用VB实现客户——服务器(TCP/IP)
用VB制作注册软件的方法

题目筛选器
日期:
类型:
状态:
得分: <=
分类:
作者:
职业:
关键字:
搜索

 
 
 
  焦点事件
 
  知识体系
 
  职业列表
 
 
  最热文章
 
 
  最多引用文章
 
 
  最新文章
 
 
 
 
网站介绍 | 广告服务 | 招聘信息 | 保护隐私权 | 免责条款 | 法律顾问 | 意见反馈
版权所有 不得转载
沪ICP备 10203777 号 联系电话:021-54428255
  帮助提示    
《我的太学》是一种全新的应用,您在操作中遇到疑问或者问题,请拨打电话13564659895,15921448526。
《我的太学》