In C# language, there is also an array of key/value combinations organized for fast search. This array is called associative array, also called hashtable.
The hash table is also in the namespace and is used to process and represent key/value pairs similar to key/value. Keys are usually used for quick searches, while keys are case-sensitive and keys must be unique. It has no valid sorting, what is done is an intrinsic sorting, and the value is used to store the value corresponding to the key. The key/value key value pairs in the hash table are both object types, so the hash table can support any type of key/value key value pairs. Each element of the hash table is a key-value pair stored in the DictionaryEntry object (the so-called DictionaryEntry structure defines a dictionary key-value pair that can be set or retrieved, with a key attribute and a value attribute, which represents the key and value respectively).
The biggest advantage of hash table is that it greatly reduces the time spent on data storage and searching, which can almost be regarded as constant time, and the cost is only the consumption of more memory. However, with the current situation where more and more memory is available, it is worth it to exchange space for time. In addition, coding is easier and it is also one of its characteristics.
1. Adding Hashtable elements
Hashtable provides an Add method for adding key/value key-value pairs to add elements. This method has two parameters. One is a key, which is equivalent to the index in the array to help find, and the other is a value. It can be regarded as an element in the array. Its format is: Hashtable object.Add(key, value)
Example 1: Use the above method to add elements of Hashtable objects
using System; using ;//The namespace that needs to be addedusing ; using ; using ; using ; namespace Hash table { class Program { static void Main(string[] args) { Hashtable al = new Hashtable(); ("The number of elements before adding al is:"+); ("1", "a"); ("2", "b"); ("3", "c"); ("The number of elements of al after adding is:"+); (); } } }
The output result is: the number of elements before adding al is: 0
The number of elements of al after adding is: 3
2. Deletion of Hashtable elements
The elements of the Hashtable object can be deleted through the Remove method and the Clear method.
(1).Clear method will clear all elements, its format is: Hashtable object.Clear()
(2).The Remove method accepts a key parameter, and its function is to remove a key/value key-value pair, and its format is: Hashtable object.Remove()
Example 2: Use the above method to delete Hashtable elements
using System; using ;//The namespace that needs to be addedusing ; using ; using ; using ; namespace Hash table { class Program { static void Main(string[] args) { Hashtable al = new Hashtable(); ("The number of elements before adding al is:"+); ("1", "a"); ("2", "b"); ("3", "c"); ("The number of elements of al after adding is:"+); ("3"); ("The number of elements of al after deleting 3 is:"+); (); } } }
The output result is: the number of elements before adding al is: 0
The number of elements of al after adding is: 3
After deleting C, the number of elements of al is: 2
3. Traversal of Hashtable elements
DictionaryEntry (dictionary key/value pair) Object is required to traverse the hash table.
Example 3: Use the foreach statement to traverse the hash table
using System; using ;//The namespace that needs to be addedusing ; using ; using ; using ; namespace Hash table { class Program { static void Main(string[] args) { Hashtable al = new Hashtable(); ("The number of elements before adding al is:"+); ("1", "a"); ("2", "b"); ("3", "c"); ("The number of elements of al after adding is:"+); foreach (DictionaryEntry t in al) { ("Key:"++"The value is:"); (); } (); } } }
The output result is: the number of elements before adding al is: 0
The number of elements of al after adding is: 3
Key bit: 1 Value is: a
Key bit: 2 Value is: b
Key bit: 3 Value is: c
4. Search for Hashtable elements
The Hashtable collection provides three search methods to find elements in a Hashtable. These three methods are the Contains method, the ContainsKe and method and the ContainsValue method.
Contains method, ContainsKey method is to search based on the key value of the Hashtable. If found, it returns the index from 0 of the last item that matches, otherwise -1 is returned, and its format is:
Hashtable object.Contains(key value) or Hashtable object.ContainsKey(key value)
The ContainValue method is to search based on the value of the Hashtable. If it is found, it returns the index of the last item that matches from 0. Otherwise, it returns -1, and its format is: Hashtable object.ContainsValue(Value value)
Example 4: Use the above method to search for Hashtable elements
using System; using ;//The namespace that needs to be addedusing ; using ; using ; using ; namespace Hash table { class Program { static void Main(string[] args) { Hashtable al = new Hashtable(); ("The number of elements before adding al is:"+); ("1", "a"); ("2", "b"); ("3", "c"); ("The number of elements of al after adding is:"+); if (("1")) { ("1 exists in al"); } if (("2")) { ("2 exists in al"); } if (("c")) { ("c exists in al"); } (); } } }
The output result is: the number of elements before adding al is: 0
The number of elements of al after adding is: 3
1 exists in al
2 exists in al
c exists in al
The above is a related introduction to C# hash table, I hope it will be helpful to everyone's learning.