Hashtable: It's a really nice thing. It's useless. I'll try it again.

Source: Internet
Author: User

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.