This article transferred from: http://www.cnblogs.com/gsk99/archive/2011/08/28/2155988.html
The following are the procedures used in DBHelper in PetShop:
Create hash table Hashtable to store cached parameters
private static Hashtable Parmcache = hashtable.synchronized (New Hashtable ());
...
Cache data
String CacheKey = "xxxx"
sqlparameter[] commandparameters= xxx;
Parmcache[cachekey] = commandparameters;
...
Get Data
String CacheKey = "xxxx"
Cachedparms = (sqlparameter[]) Parmcache[cachekey];
other information about the hash table:
a hash Table (Hashtable) Summary
In the. NET framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key/value key-value pairs, where key is typically used for quick lookups, while key is case-sensitive ; value is used to store the value corresponding to key. Key/value Key-value pairs in Hashtable are all object types, so hashtable can support any type of Key/value key-value pair.
simple operation of two hash tables
Add a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);
Remove a Key/value key value pair in the hash table: Hashtableobject.remove (key);
Remove all elements from the hash table: Hashtableobject.clear ();
Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);
The following console program will contain all of the above actions:
Using System;
Using System.Collections; This namespace must be introduced when using Hashtable
Class Hashtable
{
public static void Main ()
{
Hashtable ht=new Hashtable (); Create a Hashtable instance
Ht. Add ("E", "E");//adding Key/value key-value pairs
Ht. ADD ("A", "a");
Ht. ADD ("C", "C");
Ht. ADD ("B", "B");
String s= (String) ht["A";
if (HT. Contains ("E"))//Determine if the hash table contains a specific key with a return value of TRUE or False
Console.WriteLine ("The E key:exist");
Ht. Remove ("C");//Remove a Key/value key-value pair
Console.WriteLine (ht["A"]);//Output a here
Ht. Clear ();//Remove all elements
Console.WriteLine (ht["A"]); There will be no output here
}
}
three-traversal hash table
Traversing a hash table requires the use of DictionaryEntry Object, the code is as follows:
For (DictionaryEntry de in HT)//HT as a Hashtable instance
{
Console.WriteLine (DE. Key);//de. Key corresponds to the Key/value key value pair key
Console.WriteLine (DE. Value);//de. Key corresponds to the value of the Key/value key
}
Four sorting a hash table
Sorting the hash table here is defined as the key in the Key/value key-value pair is rearranged by certain rules, but in practice this definition is not possible because we cannot rearrange the key directly in Hashtable, If you need hashtable to provide output for some sort of rule, you can use a workaround:
ArrayList akeys=new ArrayList (ht. Keys); Don't forget to import the System.Collections
Akeys. Sort (); Sort by alphabetical order
For (string skey in Akeys)
{
Console.Write (Skey + ":");
Console.WriteLine (Ht[skey]);//post-order output
}
[Use of a hash table in the turn]net Hashtable