- 浏览: 400931 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
As we all know that Dcitinoary provide performance with some sacrifices/overhead on the space, normally the redundancy of space is needed because it has to decrease the amount of contention between different elemetns.
While HybridDictionary provides a compromise (actually it is not a compromise, it works in that way that if the size of elements is small, then it uses ListDictionary -- we will come to list dictionary later, but if thesize of elements are growing , then it will in turns uses HashTable...)
Let's see the code,
public class Class1 { public static void Main(string[] args) { HybridDictionary myCol = new HybridDictionary(); myCol.Add("Braeburn Apples", "1.49"); myCol.Add( "Fuji Apples", "1.29" ); myCol.Add( "Gala Apples", "1.49" ); myCol.Add( "Golden Delicious Apples", "1.29" ); myCol.Add( "Granny Smith Apples", "0.89" ); myCol.Add( "Red Delicious Apples", "0.99" ); myCol.Add( "Plantain Bananas", "1.49" ); myCol.Add( "Yellow Bananas", "0.79" ); myCol.Add( "Strawberries", "3.33" ); myCol.Add( "Cranberries", "5.98" ); myCol.Add( "Navel Oranges", "1.29" ); myCol.Add( "Grapes", "1.99" ); myCol.Add( "Honeydew Melon", "0.59" ); myCol.Add( "Seedless Watermelon", "0.49" ); myCol.Add( "Pineapple", "1.49" ); myCol.Add( "Nectarine", "1.99" ); myCol.Add( "Plums", "1.69" ); myCol.Add( "Peaches", "1.99" ); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine( "Displays the elements using foreach:" ); PrintKeysAndValues1( myCol ); // Display the contents of the collection using the enumerator. Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" ); PrintKeysAndValues2( myCol ); // Display the contents of the collection using the Keys, Values, Count, and Item properties. Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" ); PrintKeysAndValues3( myCol ); // Copies the HybridDictionary to an array with DictionaryEntry elements. DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count]; myCol.CopyTo( myArr, 0 ); // Displays the values in the array. Console.WriteLine( "Displays the elements in the array:" ); Console.WriteLine( " KEY VALUE" ); for ( int i = 0; i < myArr.Length; i++ ) Console.WriteLine( " {0,-25} {1}", myArr[i].Key, myArr[i].Value ); Console.WriteLine(); // Searches for a key. if ( myCol.Contains( "Kiwis" ) ) Console.WriteLine( "The collection contains the key \"Kiwis\"." ); else Console.WriteLine( "The collection does not contain the key \"Kiwis\"." ); Console.WriteLine(); // Deletes a key. myCol.Remove( "Plums" ); Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" ); PrintKeysAndValues1( myCol ); // Clears the entire collection. myCol.Clear(); Console.WriteLine( "The collection contains the following elements after it is cleared:" ); PrintKeysAndValues1( myCol ); } public static void PrintKeysAndValues1(IDictionary myCol) { Console.WriteLine(" KEY VALUE"); foreach (DictionaryEntry de in myCol) { Console.WriteLine(" {0,-25} {1}", de.Key, de.Value ); } Console.WriteLine(); } public static void PrintKeysAndValues2(IDictionary myCol) { Console.WriteLine(" KEY VALUE"); IDictionaryEnumerator enumerator = myCol.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(" {0,-25} {1}", enumerator.Key, enumerator.Value); } Console.WriteLine(); } public static void PrintKeysAndValues3(IDictionary myCol) { String[] myKeys = new string[myCol.Count]; myCol.Keys.CopyTo(myKeys, 0); // you can also use the Array.Copy to, but this is much better way to use Console.WriteLine(" INDEX KEY VALUE"); for (int i = 0; i < myCol.Count; ++i) { Console.WriteLine(" {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]]); } Console.WriteLine(); } }
To bear in mind, you will use the HybridDictionary only when you feel there is a performance bottleneck and the mitigate risk of using a general dictionary for a small set of data or risk of using a ListDictionary for a large set of data.
You can find some more information about the use of HybridDictionary at HybridDictionary class
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1929Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6554Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12054标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1251DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10953In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1293If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 939When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 818I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1925You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1717P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6099NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1227It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1098WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 870In WCF, primitives type are s ...
相关推荐
除此之外,System.Collections.Specialized 命名空间还包含了其他的集合类,如 HybridDictionary,当元素数量较少时,它以更高效的方式运作,随着元素增加,自动转换为 Hashtable。 最后,我们提到了自定义泛型集合...
对于存储字符串,可以使用System.Collections.Specialized命名空间下的StringCollection。 2. **选择合适的字典类型**: 使用Hashtable可以提升字典操作的性能,而不是使用其他特定类型的字典,如StringDictionary、...
NameValueCollection是.NET Framework中System.Collections.Specialized命名空间下的一种数据结构,它被设计用来存储一组键值对,其中键和值都是字符串。这个类在处理配置文件、HTTP表单数据或者任何需要以名称-值对...
10.6 System.Collections.Generics 299 10.6.1 泛型的定义 299 10.6.2 泛型列表 300 10.6.3 泛型字典 302 10.6.4 其他泛型集合 303 10.7 集合从.NET 1.1到.NET 2.0的变化 303 10.8 集合和列表总结 303 10.9 ...
为了提高性能,推荐使用`System.Collections.Specialized.StringCollection`来代替`ArrayList`来存储字符串,因为`StringCollection`是专门为存储字符串设计的,它内部实现了更高效的字符串管理机制。 此外,对于...
C# 2.0语言的改进1067 A.1 概述1067 A.2 泛型1068 A.2.1 泛型集合1068 A.2.2 泛型方法1072 A.3 迭代器1074 A.4 匿名方法1075 A.5 运算符重载1077 A.6 全局命名空间限定符1077 A.7 部分类1078 A.8 Visual Basic XML...
标题中提到的“C#实现读取注册表监控当前操作系统已安装软件变化的方法”涉及的知识点主要围绕在C#编程语言中对操作系统的注册表进行读取以及监控注册表中已安装软件的变化。注册表是Windows操作系统中存储配置信息...
在C#编程中,优化代码性能是提升应用程序效率的关键。以下是一些高效C#编码的优化原则,这些原则有助于减少资源消耗,提高代码运行速度。 1. **Foreach VS For 语句**: - 在遍历集合时,`foreach` 通常比 `for` ...
C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解 C# 中有多种集合类,每种集合类都有其特点和应用场景。了解这些集合类的区别可以帮助开发者选择合适的集合类来提高程序的性能和可读性。本文将介绍 ...
2. **使用Hashtable代替特定字典类型**: 例如,使用Hashtable替代StringDictionary, NameValueCollection, HybridDictionary等,因为Hashtable在某些情况下有更优的性能表现。 3. **声明字符串常量**: 对于经常使用...
最后,HybridDictionary结合了Hashtable和ListDictionary的优点,初始时使用ListDictionary,当数据量增大时切换到Hashtable,以平衡性能和内存使用。 在选择这些集合类时,开发者应考虑以下因素:数据类型、是否...