C # traverse Hashtable

Source: Internet
Author: User

This article from: http://www.cnblogs.com/cyccess/archive/2011/05/16/2047672.html

 

 

I haven't used a dictionary for a long time. I need to use it today. I don't know how to use it. The stored Traversal method!

I found my learning on the Internet. Here is a record. If I forget it, I will check it :)

It's actually very easy!

Let me remind you again,

1) HashTable stores unordered data, that is, the key and value pairs that are first stored are not necessarily in the first position;

2) HashTable stores the weak type, that is, it needs to control the storage type by itself, and mandatory type conversion is required when the value is set;

This is because Microsoft's method description is not clear enough. If it is clear, you can find the method from the description:

Create a HashTable object first:

System. Collections. HashTable ht = new System. Collections. HashTable ();

Ht. Add ("key_A", "value_A ");

Ht. Add ("key_ B", "value_ B ");

Ht. Add ("key_C", "value_C ");

Method 1:

System. Collections. IDictionaryEnumerator ht = ht. GetEnumerator ();
While (ht. MoveNext ())
{
Ht. Key. ToString (); // Key

Ht. Value. ToString (); // Value

// The current key and value have been obtained here. The output and storage are based on your own.
}

Method 2:

Foreach (System. Collections. DictionaryEntry item in ht)
{
Item. Key. ToString (); // Key

Item. Value. ToString (); // Value

// The current key and value have been obtained here. The output and storage are based on your own.

}

 

I haven't used a dictionary for a long time. I need to use it today. I don't know how to use it. The stored Traversal method!

I found my learning on the Internet. Here is a record. If I forget it, I will check it :)

It's actually very easy!

Let me remind you again,

1) HashTable stores unordered data, that is, the key and value pairs that are first stored are not necessarily in the first position;

2) HashTable stores the weak type, that is, it needs to control the storage type by itself, and mandatory type conversion is required when the value is set;

This is because Microsoft's method description is not clear enough. If it is clear, you can find the method from the description:

Create a HashTable object first:

System. Collections. HashTable ht = new System. Collections. HashTable ();

Ht. Add ("key_A", "value_A ");

Ht. Add ("key_ B", "value_ B ");

Ht. Add ("key_C", "value_C ");

Method 1:

System. Collections. IDictionaryEnumerator ht = ht. GetEnumerator ();
While (ht. MoveNext ())
{
Ht. Key. ToString (); // Key

Ht. Value. ToString (); // Value

// The current key and value have been obtained here. The output and storage are based on your own.
}

Method 2:

Foreach (System. Collections. DictionaryEntry item in ht)
{
Item. Key. ToString (); // Key

Item. Value. ToString (); // Value

// The current key and value have been obtained here. The output and storage are based on your own.

}

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.