[C#] C# foreach语句中使用break暂停遍历 →→→→→进入此内容的聊天室

来自 , 2019-05-06, 写在 C#, 查看 114 次.
URL http://www.code666.cn/view/b742027d
  1. using System;
  2.  
  3. public class w3demo {
  4.   public static void Main() {
  5.     int sum = 0;
  6.     int[] nums = new int[10];
  7.  
  8.     // give nums some values
  9.     for(int i = 0; i < 10; i++)  
  10.       nums[i] = i;
  11.  
  12.     // use foreach to display and sum the values
  13.     foreach(int x in nums) {
  14.       Console.WriteLine("Value is: " + x);
  15.       sum += x;
  16.       if(x == 4) break; // 当x=4时停止遍历
  17.     }
  18.     Console.WriteLine("Summation of first 5 elements: " + sum);
  19.   }
  20. }
  21. //csharp/7627

回复 "C# foreach语句中使用break暂停遍历"

这儿你可以回复上面这条便签

captcha