`

c# - HybridDictionary in System.Collections.Specialized

    博客分类:
  • C#
c# 
阅读更多

 

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

分享到:
评论

相关推荐

    c#集合的学习读书笔记 学习心得

    除此之外,System.Collections.Specialized 命名空间还包含了其他的集合类,如 HybridDictionary,当元素数量较少时,它以更高效的方式运作,随着元素增加,自动转换为 Hashtable。 最后,我们提到了自定义泛型集合...

    提高.net运行速度

    对于存储字符串,可以使用System.Collections.Specialized命名空间下的StringCollection。 2. **选择合适的字典类型**: 使用Hashtable可以提升字典操作的性能,而不是使用其他特定类型的字典,如StringDictionary、...

    NameValueCollection.zip

    NameValueCollection是.NET Framework中System.Collections.Specialized命名空间下的一种数据结构,它被设计用来存储一组键值对,其中键和值都是字符串。这个类在处理配置文件、HTTP表单数据或者任何需要以名称-值对...

    2.ASP.NET.2.0.高级编程(第4版) [1/7]

    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 ...

    asp.net编译运行速度.txt

    为了提高性能,推荐使用`System.Collections.Specialized.StringCollection`来代替`ArrayList`来存储字符串,因为`StringCollection`是专门为存储字符串设计的,它内部实现了更高效的字符串管理机制。 此外,对于...

    ASP.NET2.0高级编程(第4版)1/6

    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#实现读取注册表监控当前操作系统已安装软件变化的方法”涉及的知识点主要围绕在C#编程语言中对操作系统的注册表进行读取以及监控注册表中已安装软件的变化。注册表是Windows操作系统中存储配置信息...

    高效C#编码优化原则

    在C#编程中,优化代码性能是提升应用程序效率的关键。以下是一些高效C#编码的优化原则,这些原则有助于减少资源消耗,提高代码运行速度。 1. **Foreach VS For 语句**: - 在遍历集合时,`foreach` 通常比 `for` ...

    C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解

    C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解 C# 中有多种集合类,每种集合类都有其特点和应用场景。了解这些集合类的区别可以帮助开发者选择合适的集合类来提高程序的性能和可读性。本文将介绍 ...

    提高asp.net编译运行速度

    2. **使用Hashtable代替特定字典类型**: 例如,使用Hashtable替代StringDictionary, NameValueCollection, HybridDictionary等,因为Hashtable在某些情况下有更优的性能表现。 3. **声明字符串常量**: 对于经常使用...

    详解如何选择使用ArrayList、HashTable、List、Dictionary数组

    最后,HybridDictionary结合了Hashtable和ListDictionary的优点,初始时使用ListDictionary,当数据量增大时切换到Hashtable,以平衡性能和内存使用。 在选择这些集合类时,开发者应考虑以下因素:数据类型、是否...

Global site tag (gtag.js) - Google Analytics