C#使用foreach語句遍歷數組的代碼

下面的內容內容是關於C#使用foreach語句遍歷數組的內容,希望對大家有所好處。

using System; 

public class w3demo { 
  public static void Main() { 
    int sum = 0; 
    int[] nums = new int[10]; 

    for(int i = 0; i < 10; i++)  
      nums[i] = i; 

    foreach(int x in nums) { 
      Console.WriteLine("Value is: " + x); 
      sum += x; 
    } 
    Console.WriteLine("Summation: " + sum); 
  } 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章