c#foreach的使用
Loops are an important part of the programming languages. For
and While
loops are used for decades in different programming languages. Foreach
provides similar functionality in a more elegant way by iterating over the given list. In this tutorial, we will examine foreach
in different programming languages like PHP, JavaScript, Java, C# and Python.
循环是编程语言的重要组成部分。 For
和While
循环在不同的编程语言中使用了数十年。 通过遍历给定列表, Foreach
以更优雅的方式提供了类似的功能。 在本教程中,我们将研究使用不同编程语言(例如PHP,JavaScript,Java,C#和Python)的foreach
。
PHP的Foreach的 (PHP Foreach)
PHP provides foreach in order to iterate over given arrays. There is two main usage syntax about PHP foreach.
PHP提供了foreach以便迭代给定的数组。 有关PHP foreach的主要用法有两种。
值语法 (Value Syntax)
In value syntax, we will provide just the array which is named ARRAY_EXPRESSION and the VALUE. We will write operations to the STATEMENT
用值语法,我们将只提供名为ARRAY_EXPRESSION的数组和VALUE。 我们将操作写入STATEMENT
foreach (ARRAY_EXPRESSION as $VALUE)
STATEMENT
In this example, we will print array values to the standard output.
在此示例中,我们将数组值打印到标准输出。
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
?>
键,值语法 (Key, Value Syntax)
In Key and Value syntax will also get the key from the array and use it in the foreach.
在“键和值”中,语法还将从数组中获取键,并在foreach中使用它。
foreach (ARRAY_EXPRESSION as $KEY=>$VALUE)
STATEMENT
In this example, we will print key and values into standard output
在此示例中,我们将键和值打印到标准输出中
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $key=>$value) {
echo $value;
}
?>
JavaScript Foreach (JavaScript Foreach)
JavaScript is a very extensible language where most of the features are provided by objects. foreach
is also provided by the object which is an array. It has the following syntax.
JavaScript是一种非常可扩展的语言,其中的大多数功能都是由对象提供的。 foreach
也由数组对象提供。 它具有以下语法。
JavaScript Foreach语法 (JavaScript Foreach Syntax)
ARRAY.forEach(function(VALUE) {
STATEMENT;
});
In this example, we will iterate over array named arr
and print to the console.
在此示例中,我们将遍历名为arr
数组并打印到控制台。
var arr = ['a', 'b', 'c','d','e']
arr.forEach(function(element) {
console.log(element);
});
Java Foreach的 (Java Foreach)
Java programming language actually does not provides native foreach
keyword. But we can use other elements of Java like List
, ArrayList
and for
.
Java编程语言实际上不提供本机的foreach
关键字。 但是我们可以使用Java的其他元素,例如List
, ArrayList
和for
。
对于语法 (For Syntax)
We will use List
and for
to create foreach
mechanism. TYPE is VALUE type and we will provide LIST.
我们将使用List
和for
创建foreach
机制。 TYPE是VALUE类型,我们将提供LIST。
for(TYPE VALUE:LIST){
STATEMENT
}
We will iterate over string list and we will print to the standard output.
我们将遍历字符串列表,并打印到标准输出。
List<String> items = new ArrayList<>("A","B","C","D");
for(String item : items){
System.out.println(item);
}
Foreach语法 (Foreach Syntax)
After Java 8 List
type provides forEach()
function. So we can create a LIST and then iterate over elements with forEach()
function. We will also use a lambda expression.
Java 8之后, List
类型提供了forEach()
函数。 因此,我们可以创建一个LIST,然后使用forEach()
函数遍历元素。 我们还将使用lambda表达式。
List<String> items = new ArrayList<>("A","B","C","D");
items.forEac(item->System.out.println(item));
C#的Foreach (C# Foreach)
C # provides the foreach
statement in a native way. We will provide the LIST with the in
keyword and set ELEMENT with the given TYPE.
C#以本机方式提供了foreach
语句。 我们将为LIST提供in
关键字,并为ELEMENT设置TYPE。
foreach (TYPE ELEMENT in LIST){
STATEMENT
}
In this example, we will iterate over an integer list named lst
.
在此示例中,我们将遍历一个名为lst
的整数列表。
var lst= new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in lst) {
Console.WriteLine($"Element {element}");
}
Python Foreach (Python Foreach)
Python programming language does not provide the foreach
keyword but the actual implementation of the for
in Python is hte same as the foreach
. We can iterate over List
, Array
, Dictionary
type in python with for
like below.
Python编程语言不提供foreach
关键字,但是for
在Python中的实际实现与foreach
相同。 我们可以使用下面的for
迭代python中的List
, Array
, Dictionary
类型。
for ELEMENT in LIST:
STATEMENT
In this example, we will iterate over list named lst
.
在此示例中,我们将遍历名为lst
列表。
lst=[1,2,3,4,5]
for element in lst:
print element
c#foreach的使用