The foreach Traversal method of collection and dictionary in C #

Source: Internet
Author: User

For several built-in collection classes in. NET Framework, foreach is a convenient Traversal method:

1. Non-generic & Weak collections (arraylist, queue, stack ):

Use object:

Arraylist Al = new arraylist ();
Al. Add ("hello ");
Al. Add (1 );
Foreach (Object OBJ in Al)
{
Console. writeline (obj. tostring ());
}

If you confirm the type in the arraylist, you can also use this type proxy to automatically convert. However, if the conversion fails, an invalidcastexception is thrown.

Arraylist Al = new arraylist ();
Al. Add ("hello ");
Al. Add ("world ");
Foreach (string s in Al)
{
Console. writeline (s );
}

2. A strong type of collections (stringcollection and bitarray) can use string and bool without the need for strong conversion.

Non-generic dictionaris (hashtable, sortedlist, stringdictionary, etc ):

Use dictionaryentry:

Hashtable ht = new hashtable ();
Ht. Add (1, "hello ");
Ht. Add (2, "world ");
Foreach (dictionaryentry de in HT)
{
Console. writeline (De. value );
}

Special Dictionary (namevaluecollection ):

You cannot perform foreach traversal on namevaluecollection directly. Two levels are required:

Namevaluecollection NVC = new namevaluecollection ();
NVC. Add ("A", "hello ");
NVC. Add ("A", "world ");
NVC. Add ("B ","! ");
Foreach (string key in NVC. allkeys)
{
Foreach (string value in NVC. getvalues (key ))
{
Console. writeline (value );
}
}

Generic collections

List <t>, queue <t>, stack <t>: foreach T.

Dictionary <tkey, tvalue> and sortedlist <tkey, tvalue> Use keyvaluepair <tkey, tvalue>:

Dictionary <int, string> DIC = new dictionary <int, string> ();
Dic. Add (1, "hello ");
Dic. Add (2, "world ");
Foreach (keyvaluepair <int, string> pair in DIC)
{
Console. writeline (pair. value );
}

Note: During the foreach process, changing the length of the Collection class will lead to errors. Therefore, do not add or remove the collection class in the loop body of the foreach. Dictionary <tkey, tvalue> is non-thread-safe. Using foreach for multiple threads may cause errors. We recommend that you use non-generic hashtable (or lock yourself...) When multithreading ..).

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.