C# 添加到列表时,如何跳过每9项? 公共无效图像链接 { 整数计数=0; DatesAndTimes中的foreachstring c { 如果计数=9 { 计数=0; } 字符串imageUrl=firstUrlPart+countriescodes[count]+secondUrlPart+DatesAndTimes[count]+thirdUrlPart+true; imagesUrls.AddimageUrl; 计数++; } }

C# 添加到列表时,如何跳过每9项? 公共无效图像链接 { 整数计数=0; DatesAndTimes中的foreachstring c { 如果计数=9 { 计数=0; } 字符串imageUrl=firstUrlPart+countriescodes[count]+secondUrlPart+DatesAndTimes[count]+thirdUrlPart+true; imagesUrls.AddimageUrl; 计数++; } },c#,.net,winforms,C#,.net,Winforms,列表日期和时间的格式如下:每一行都是列表中的项目 201612281150201612281150 201612281150201612281151 201612281150201612281152 201612281150201612281153 201612281150201612281154 201612281150201612281155 201612281150201612281156 201612281150201612281157 201612281150201612281158

列表日期和时间的格式如下:每一行都是列表中的项目

201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Europe
201612281150201612281150
201612281150201612281151
201612281150201612281152
201612281150201612281153
201612281150201612281154
201612281150201612281155
201612281150201612281156
201612281150201612281157
201612281150201612281158
201612281150201612281159
Turkey
每10项中就有一个名称。 我想在构建链接时跳过名称。 因此,我每次从0到9计数10次,然后我将计数器重置为0,我还想跳过名称,例如,欧洲,然后再次计数到10,跳过土耳其,等等。

在if语句中使用continue。它将在之后跳过该行,并恢复foreach循环的执行

public void ImagesLinks()
            {
                int count = 0;
                foreach(string c in DatesAndTimes)
                {
                    if (count == 9)
                    {
                        count = 0;
                        continue;
                    }
                    string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
                    imagesUrls.Add(imageUrl);
                    count++;
                }
            }
您还可以使用计数%9==0,这样您就不必重置计数器:

public void ImagesLinks()
{
    int count = 0;
    foreach(string c in DatesAndTimes)
    {
        if (count++ % 9 == 0)
            continue;

        string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true";
        imagesUrls.Add(imageUrl);
    }
}

过滤掉包含字母的字符串不是更容易吗?可以使用count对两个不同的数组countriescodes和DatesAndTimes进行索引。看起来要么你有一个大数组,每个国家有9次,要么你应该得到IndexOutfrange例外。而且你从不使用c。我会完全修改这个代码。