For can not traverse one by one. For example, you can traverse one by one, or from the front to the back. However, foreach cannot traverse from the back to the back, but can only traverse from the back to the back, and foreach can only traverse one by one. You cannot assign values to iteration variables in foreach.
Note:
The foreach object is a set.
The set is defined below.
Arrays and lists are collections. Go to msdn for details. The above is comprehensive.
The following example prints the values of each element in a two-dimensional array in order of elements:
Using system;
Class Test
{
Static void main (){
Double [,] values = {
{1.2, 2.3, 3.4, 4.5 },
{5.6, 6.7, 7.8, 8.9}
};
Foreach (double elementvalue in values)
Console. Write ("{0}", elementvalue );
Console. writeline ();
}
}
Differences from for loop:
The for circulating oil condition judgment can restrict the execution conditions. No. Foreach is more commonly used to traverse collection objects.