Summary of methods for traversing various data sets in C #

Source: Internet
Author: User

Summary of methods for traversing various types of data collections in C #:
1. Enumeration type

 //  Iterate through enumeration types the enumeration names of sample  foreach  ( string  sp in  enum.getnames (typeof   //  Iterate through enumerated types enumeration values for sample  foreach  (string  sp in  enum.getvalues (typeof  

2. Traverse ArrayList (Queue, Stack)
In this case, for example, the elements in ArrayList can be any data type, and it is necessary to verify that the elements in the ArrayList are of the same data type.

  foreach (string in 

In addition to traversing the queue and stack stacks in the same way as ArrayList, you can use foreach to iterate through, except that one is advanced first out and the other is advanced.

New ArrayList (); // For traversal  for (int0; i < list.) Count; i++) {  = (SE) list[i];  Console.WriteLine (SE. Name);} // foreach Traversal foreach inch list) {  = (SE) list[i];  Console.WriteLine (SE. Name);}


controls in a 3.Winform form

 //  traverse the search for controls in the main form and remove the eligible controls from the form  foreach  (Control ctl in  this   //  gets and determines the control type or control name  if  (ctl. GetType (). Name.equals ( " listbox   ") | | Ctl. Name.equals ( " listbox1  "   this  . Controls.remove (CTL); } 

4.HashTable Hash Table
The DictionaryEntry class needs to reference System.Collections

//traversing keys and values in a full hash tableforeach(DictionaryEntry IteminchhashTable) {ary. ADD ("Hash Key:"+item. key+", hash value:"+item. Value.tostring ()); You can also traverse the keys or values in the hash table independently. //only the keys in the hash table are traversedforeach(stringKeyinchhashtable.keys) {ary. ADD ("Hash Key:"+key); } //traverse only the values in the hash tableforeach(stringValueinchhashtable.values) {ary. ADD ("Hash Value:"+value); } 

5. Traversing rows and columns in a dataset and DataTable

//traversing a table in a datasetforeach(DataTable DTinchdataset.tables) {ary. ADD ("Table Name:"+dt. Tablename.tostring ()); } //traverse rows in the default first table in a datasetforeach(DataRow Drinchdataset.tables[0]. Rows) {//get data for a field (column) in a rowary. ADD (dr["ID"]. ToString ()); } //iterate through columns in the default first table in a datasetforeach(DataColumn Colinchdataset.tables[0]. Columns) {ary. ADD ("Column Name:"+Col. ColumnName); } 

A DataTable iterates through rows and columns in a similar way to a dataset, just by replacing the dataset.tables[0] with a specific table.
You can also make a SQL query on a DataTable table and then iterate over the results of the query.

foreach in dataset.tables[0].   Select (""///ary. ADD (dr["ID"

6. Traversing rows in the DataGridView

 foreach in//ary.   ADD (Dr. cells["ID"

7. Traverse the listbox and the item in the ComboBox
The generic foreach traversal can only traverse the name of the item in the ListBox and ComboBox, and the full traversal of the item data that needs to be added when binding the item is an object of a two-tuple property custom class that takes the name of one property in the object as DisplayMember ( Item name), and the other as the Displayvalue (item value). This allows you to retrieve the names and values of the ListBox and the item in the ComboBox when traversing.

8.list<t>

// For traversal  for (int0; i < list.) Count; i++) {  // traversal does not require type conversion   Console.WriteLine (List[i]);} // foreach Traversal foreach inch list) {  // traversal does not require type conversion   Console.WriteLine (obj);}

9.dictionary<k,v>

// Traverse Values foreach inch list. Values) {  // traversal does not require type conversion    // traversal of foreach at the same time  ( keyvaluepair<string in list {  Console.WriteLine (en). Key);  Console.WriteLine (en. Value.name);} // keyvaluepair<tkey,tvalue> is a generic structure

Summary of methods for traversing various data sets in C #

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.