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.
}