I used this item when I used to analyze the data. At that time, I even called it a few times. Today, some friends asked me about this and I would like to add some more.
System. collections. hashtable is a container used to represent a group of key/value structures. It may be called map or dictionary in other languages, and the key in its structure is used for quick search, it may be more appropriate to call a dictionary.
Hashtable does not have many methods and attributes. Most of them can also be regarded as justice, but hashtable provides the hashtable [akey] method to reference its contained objects, however, there is no index pointing to the number. That is to say, we are used to arrays. We cannot use hashtable [0] or other methods to retrieve its content, we cannot use it either.
For (INT I = 0; I
{
Console. writeline ("key -- {0}; Value -- {1}.", hashtable [I]. Key, hashtable [I]. value );
}
This code block is used to traverse data. However, if we add key-value pairs to hashtable, in many cases, we will eventually traverse the key-value pairs. How can this problem be solved? The following code will answer this question.
Foreach (dictionaryentry de in hashtable)
{
Console. writeline ("key -- {0}; Value -- {1}.", De. Key, De. value );
}
However, if you are still accustomed to using index for access, system. Collections. Specialized. namevaluecollection is a class that can be accessed using index. It is uncomfortable that its key/value must be of the string data type.
Remember that system. Collections. Specialized. listdictionary is a one-way linked list structure. Therefore, if there are not many key/value pairs, it is more efficient than hashtable.
System. Collections. Specialized. stringdictionary, the key is a case-sensitive string data type.
Hashtable hash = new hashtable ();
For (INT I = 0; I <ID. length; I ++)
{
If (ID [I]. Trim ()! = "")
{
String builid = ID [I];
String buildept = G. getxxxx (builid). Rows [0] ["XXXX"]. tostring (). Trim ();
If (! Hash. Contains (buildept ))
{
Hash. Add (buildept, builid );
}
Else
{
Hash [buildept] = (string) hash [buildept] + "," + builid;
}
}
}
Foreach (dictionaryentry de in hash)
{
String dept = de. Key. tostring (). Trim ();
String IDs = de. value. tostring (). Trim ();
}