C # Basics------The difference between for and foreach

Source: Internet
Author: User

We typically use a for loop in the traversal of an array.

Example:

the traversal output of the//For statement is used
1 string[] names={"Panda","Fox","Mermaid"};2 3 for(intI=0; I<names. length;i++)4 {5 strings1=Names[i];6 console.writleline (str); 7 }8 9Console.readkey ();

There is another way to traverse the loop output in C #: foreach

foreach knows the syntax sugar of C # based on its name.

The above data is output with a foreach

1 //the traversal output of the For statement uses the2  string[] names={"Panda","Fox","Mermaid"};3 4 foreach(stringS1inchnames)5 {6 Console.WriteLine (S1); 7 }8 9Console.readkey ();

Is it comfortable to use? foreach reduces the code and increases the readability of the program.

Again, specifically, the role of foreach in implementing

Foreach,in (C # Reference)

The foreach statement repeats an embedded statement group for each element in an array or object collection. The foreach statement is used to iterate through the collection to get the information you want, but not to change the contents of the collection to avoid unpredictable side effects.

Notes

An embedded statement continues execution for each element in an array or collection. After the iteration is completed for all elements in the collection, control is passed to the next statement after the foreach block.

You can use the break keyword to jump out of a loop at any point in a foreach block, or use the Continue keyword to go directly to the next iteration of the loop.

The foreach loop can also be exited by a Goto, return, or throw statement.

For more information about the foreach keyword, including code examples, see the following topics:

Using foreach with Arrays (C # Programming Guide)

How to: Access a collection class using foreach (C # Programming Guide)

Example

In this example, you use foreach to display the contents of an array of integers.

1 //Cs_foreach.cs2 classforeachtest3 {4     Static voidMain (string[] args)5     {6         int[] Fibarray =New int[] {0,1,2,3,5,8, - };7         foreach(intIinchFibarray)8         {9 System.Console.WriteLine (i);Ten         } One     } A}
Output
01235813

Principle:

After the anti-compilation, it is concluded that:

In the Foreach loop, the process of iterating over the collection Collectionobject is as follows:
(1) Call Collectionobject.getenumerator () and return a IEnumerator reference. This method can be obtained through the implementation code of the IEnumerable interface. But this is optional.
(2) Call the MoveNext () method of the returned IEnumerator interface.
(3) if the MoveNext () method returns True, a reference to the object is obtained using the current property of the IEnumerator interface for the Foreach loop.
(4) Repeat the previous two steps until the MoveNext () method returns False, at which time the loop stops.

Such a good thing do not know C + + There is no, checked the next found that C + + does not have this keyword. but I think C + + can do this by principle.

C + + can be handled by macros

The parameters of the foreach macro are: element type, element name, container type, container name.

Suppose there is a vector like the following definition:
Names<int > V;

Then traversing v can use the following code:
foreach (int, E, names<int;, v)
{//Use (e) to access elements of V
}

end!

Welcome to Exchange, sharing the programmer inspirational story.    QQ Group of Happy programmers:

C # Basics------The difference between for and foreach

Related Article

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.