2011-01-05 140 views

回答

3
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

0

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

0

有時該解決方案是很簡單的。信息可以發現here

一個例子:

// 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