问题描述
foreach c#
中的循环如何使用推荐答案
class ForEachTest { static void Main(string[] args) { int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } } }
其他推荐答案
foreach语句重复数组或对象集合中每个元素的一组嵌入式语句. foreach语句用于通过集合进行迭代以获取所需的信息,但不应用于更改集合的内容以避免不可预测的副作用.
示例:
class ForEachTest { static void Main(string[] args) { int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } } }
msdn: https://msdn.microsoft .com/en-us/library/ttw7t8t6(v = vs.80).aspx
其他推荐答案
有时解决方案非常简单.可以找到信息在这里
一个示例:
// Use a string array to loop over. string[] ferns = { "Psilotopsida", "Equisetopsida", "Marattiopsida", "Polypodiopsida" }; // Loop with the foreach keyword. foreach (string value in ferns) { Console.WriteLine(value); }
可以在此处找到更多信息和示例: http://dotnetperls.com/foreach
问题描述
How is a foreach loop used in C#?
推荐答案
class ForEachTest { static void Main(string[] args) { int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } } }
其他推荐答案
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
Example:
class ForEachTest { static void Main(string[] args) { int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); } } }
MSDN: https://msdn.microsoft.com/en-us/library/ttw7t8t6(v=vs.80).aspx
其他推荐答案
Sometimes the solution is very simple. Info can be found here
An example:
// Use a string array to loop over. string[] ferns = { "Psilotopsida", "Equisetopsida", "Marattiopsida", "Polypodiopsida" }; // Loop with the foreach keyword. foreach (string value in ferns) { Console.WriteLine(value); }
More information and examples can be found here: http://dotnetperls.com/foreach