Welcome to Unity Learning,Unity training ,UnityEnterprise TrainingEducation Area, there are manyUnity3d Resources,Unity3d Training Video,Unity3d Tutorials,Unity3d Frequently Asked questions,Unity3d Project Source code, the "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry unity3d Training , learning the first brand.
One, hash table (Hashtable) Briefin the. NET Framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like KeyValue key-value pairs, where key is usually used for quick lookups, and key is case-sensitive; value is used to store the value corresponding to key. KeyValue Key-value pairs in Hashtable are all object types, so hashtable can support any type of KeyValue key-value pair.
two, simple operation of hash table
Add a KeyValue key value pair to the hash table: Hashtableobject.add (Key,value);
Remove a KeyValue 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; When file uses Hashtable, this namespace must be introduced
Class Hashtable
{
public static void Main ()
{
Hashtable ht=new Hashtable (); File creates a Hashtable instance
Ht. Add (e,e); adding keyvalue key-value pairs
Ht. ADD (A,a);
Ht. ADD (C,C);
Ht. ADD (B,B); string s= (String) ht[a];
if (HT. Contains (E)) file determines whether a hash table contains a specific key with a return value of TRUE or False
Console.WriteLine (the E keyexist);
Ht. Remove (C) removes a keyvalue key-value pair
Console.WriteLine (Ht[a]); here output A
Ht. Clear (); Remove all elements
Console.WriteLine (Ht[a]); File will not have any output here
}
}third, traverse the hash table
traversing a hash table requires the use of DictionaryEntry Object, the code is as follows:
For (DictionaryEntry de in HT) fileht as a Hashtable instance
{
Console.WriteLine (DE. Key);d E. Key corresponds to the KeyValue key value pair key
Console.WriteLine (DE. Value);d E. Key corresponds to the value of the KeyValue key
}
Four, sort the hash table
Sorting the hash table here is defined as the key in the KeyValue 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); File don't forget to import the System.Collections
Akeys. Sort (); File is sorted in alphabetical order
For (string skey in Akeys)
{
Console.Write (Skey +);
Console.WriteLine (Ht[skey]); Sort after output
}For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy
Usage of Hashtable in C #