Home > Backend Development > C#.Net Tutorial > Comparison between for loop and foreach loop in C#

Comparison between for loop and foreach loop in C#

黄舟
Release: 2017-05-21 10:48:34
Original
2331 people have browsed it

This article mainly introduces the related knowledge of for loop and foreach loop in C# , has very good reference value. Let’s take a look at it with the editor

The for loop and the foreach loop can actually be regarded as subordinate relationships, that is, the foreach loop can be converted into a for loop, but the for loop may not be converted into a foreach loop.

The following is a brief introduction to the two loops:

1.for loop

Code format:

for(expression1; loop condition; expression 2)

{

Loop body

}

Code meaning:

First run expression 1;

Then determine whether the condition is true. If it is true, execute the loop body. After execution Run expression 2 again;

and then judge the loop condition... The loop will not end until the loop condition is false.

Notes:

Expression 1: It can be any code, it will be executed and will only be executed once ;

Expression 2: It can be any code, executed after the loop body is executed.

2.foreach loop

Code format:

foreach(Data type Variable in array or collection)
{
loop body

}

Code meaning:

From the array or collection, take out the data of each item in turn. Each time the data of an item is taken out, assign the data to the loop variable. After each assignment, run the loop body once.

Note:

foreach loop can only be used to traverse arrays and collections;

The foreach loop is also called a read-only loop. In the loop body, the collection or array cannot be changed;

The data type must be the same as the data type of each item in the array or collection.

However, what are the differences, advantages and disadvantages between foreach loop and for loop? The following is a brief summary:

##foreach loop                                           #Can only be used for traversal; Can be used for any form of repeated behavior;Cannot change the loop target; In the loop body, any operation can be performed;

Traverse Fast speed and high execution efficiency. The traversal speed is slow and the execution efficiency is low.

Summary: If you need to traverse a collection or array, and only need to read without changing it during the traversal process, it is most appropriate to use a foreach loop. Otherwise, choose other loops as needed.

The above is the detailed content of Comparison between for loop and foreach loop in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template