In C #, the way to iterate through the various collections of data is summarized here:
1. Enumeration type
Iterate through enumeration types for each enumeration name of Sample, foreach (String sp in Enum.getnames (typeof (Sample))) { ary. ADD (SP); } Iterates over enumeration values of enum type Sample, foreach (String sp in Enum.getvalues (typeof (Sample))) { ary. ADD (SP); }
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.
The traversal element is a string of type queue foreach (string text in ArrayList) { ary. ADD (text); }
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.
Controls in a 3.Winform form
Traverse the search for a control in the main form and remove the conditional control from the form with foreach (Control ctl in). Controls) { //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
Iterate through the keys and values in the full hash table, foreach (DictionaryEntry item in hashTable) { ary. ADD ("Hash key:" +item. Key+ ", hash value:" +item. Value.tostring ()); } The keys or values in the hash table can also be traversed independently. //Traverse only the key in the hash table, foreach (string key in Hashtable.keys) { ary. ADD ("Hash keys:" + key); } Only the value in the hash table is traversed by foreach (string value in Hashtable.values) { ary. ADD ("Hash Value:" + value); }
5. Traversing rows and columns in a DataSet and DataTable
Traverse the table in the DataSet foreach (DataTable dt in dataset.tables) { ary. ADD ("Table name:" + dt.) Tablename.tostring ()); } Iterates through the rows in the default first table in the DataSet foreach (DataRow Dr in Dataset.tables[0]. rows) { //Gets the data ary of a field (column) in the row . ADD (dr["ID"]. ToString ()); } Iterates over columns in the first table in the DataSet, foreach (DataColumn col in Dataset.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.
Iterates over the results of a table select in a dataSet after executing a query condition (DataRow Dr in Dataset.tables[0]. Select ("Month>6 and Month<12")) { //Gets the data ary of a field (column) in the row . ADD (dr["ID"]. ToString ()); }
6. Traversing rows in the DataGridView
Traverse DataGridView in the row foreach (DataGridViewRow dr in Datagridview1.rows) { //Gets the data ary of a field (column) in the row . ADD (Dr. cells["ID"]. ToString ()); }
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.
The above is the C # traversal of various types of data collection method summary content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!