Differences between foreach and

Source: Internet
Author: User

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.

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.