C#中foreach語句應用break暫停遍歷的辦法。本站提示廣大學習愛好者:(C#中foreach語句應用break暫停遍歷的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中foreach語句應用break暫停遍歷的辦法正文
本文實例講述了C#中foreach語句應用break暫停遍歷的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
上面的代碼演示了在C#中應用foreach時若何經由過程break語句暫停數據遍歷
using System;
public class w3demo {
public static void Main() {
int sum = 0;
int[] nums = new int[10];
// give nums some values
for(int i = 0; i < 10; i++)
nums[i] = i;
// use foreach to display and sum the values
foreach(int x in nums) {
Console.WriteLine("Value is: " + x);
sum += x;
if(x == 4) break; // 當x=4時停滯遍歷
}
Console.WriteLine("Summation of first 5 elements:" + sum);
}
}
願望本文所述對年夜家的C#法式設計有所贊助。