In C # programming, you often need to process some key-value pairs. Generally, you can use dictionaries or hash tables. Compared with dictionaries, hash tables consume less system resources.
The following is an example of common hash table methods:
1. Add elements;
2. delete an element;
3. traversal;
4. query.
Step-by-Step reading tools/Raw Materials
Method/step
- 1
Open Microsoft Visual Studio 2010 and create a program named [hash table.
- 2
Place appropriate controls in the blank window of the new program interface: including,
Displays the Textbox Control of the operation flow;
Add four button controls, click Add, view, clear, and query ];
Two textbox controls for adding data to a user: Name and mobile phone number ];
Enter two textbox controls for data query.
Steps
- 3
Add a new hash table in the code.
Edit the Add button code to obtain user input data and add it to the hash table.
Step 1
- 4
Edit the [view] button code
Traverse the hash table and output each element.
Steps
- 5
Edit the Clear button code
Clear the hash table.
Steps
- 6
Edit the query button code
If you select a name query, the system checks whether the key of the hash table matches;
If you select a telephone query, you can determine whether the hash table value matches;
Steps
- 7
Generate an EXE file for testing,
Add data for three employees respectively.
Steps
- 8
Click View to view information about the three employees.
Steps
- 9
Select "by name", enter a name that does not exist, and click "query". "No corresponding data" is displayed ].
Steps
- 10
Enter an existing name and click query to display [corresponding data ].
Steps
- 11
Select "by phone", enter a phone number that does not exist, and click "query" to display "no data ].
Steps
- 12
Enter an existing phone number and click query. The corresponding data is displayed ].
Steps
- 13
Click Clear to clear all hash table data. Click View. No hash table data is output, indicating that the hash table has no data.
Step: Read end