简体   繁体   English

如何跳过c sharp中每个循环的特定位置?

[英]How to skip a specific position within a for each loop in c sharp?

List<string> liste = new List<String> 
        {
            "A","B","C","D"
        };

        foreach (var item in liste)
        {
            System.Diagnostics.Debug.WriteLine(item.ToString());
        }

        for (int i = 0; i < liste.Count; i++)
        {
            if (i == 0)
                continue;
            System.Diagnostics.Debug.WriteLine(liste[i].ToString());
        }

How do i skip a specific position in a foreach loop? 如何跳过foreach循环中的特定位置? I do not want to evaluate any values, but just skip the position x. 我不想评估任何值,只是跳过位置x。

It has to be a specific position. 它必须是一个特定的位置。 One could choose position 0 or maybe position 7. 可以选择位置0或位置7。

It is very easy to skip the first item in the list: 跳过列表中的第一项非常容易:

foreach(var item in list.Skip(1))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}

If you want to skip any other element at index n , you could write this: 如果你想跳过索引n处的任何其他元素,你可以这样写:

foreach(var item in list.Where((a,b) => b != n))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}

In this example I use a lambda expression that takes two arguments: a and b . 在这个例子中,我使用一个带有两个参数的lambda表达式: ab Argument a is the item itself, while argument b is the index of the item. 参数a是项本身,而参数b是项的索引。

The relevant pages on MSDN that describe these extension methods are: MSDN上描述这些扩展方法的相关页面是:

You could even write your own extension method that allows you to skip an element in a list: 您甚至可以编写自己的扩展方法,允许您跳过列表中的元素:

public static class MyEnumerableExtensions
{
    public static IEnumerable<T> SkipAt<T>(this IEnumerable<T> list, int index)
    {
        var i = 0;

        foreach(var item in list) 
        {
            if(i != index)
                yield return item;

            i++;
        }
    }
}

This will allow you to write something like this to skip an item: 这将允许你写这样的东西来跳过一个项目:

foreach(var item in list.SkipAt(2))
{
    System.Diagnostics.Debug.WriteLine(item.ToString());
}

A foreach loop iterates over a collection that implements IEnumerable . foreach循环遍历实现IEnumerable的集合。 The enumerator exposes the current item and a method to move onto the next item - it has no concept of an index. 枚举器公开当前项和移动到下一项的方法 - 它没有索引的概念。

You could always do: 你可以随时做:

var i = 0;
foreach (var item in liste) {
  if (i++ == skip) continue;
  Debug.WriteLine(item.ToString());
}

But this seems unnecessarily contrived. 但这似乎不必要地做作。 If you need an index, go with a for loop. 如果您需要索引,请使用for循环。

The other option is to remove the undesired item from the List before iterating: 另一个选项是在迭代之前从List删除不需要的项:

foreach (var item in liste.Take(n-1).Union(liste.Skip(n))) {
  Debug.WriteLine(item.ToString());
}

I love list's .ForEach, here's my take using @Elian's .SkipAt(n) and .ForEach: 我喜欢list's。ForEach,这是我使用@ Elian的.SkipAt(n)和.ForEach:

var list = new List<String> { "A", "B", "C", "D" };
list = list.SkipAt(1).ToList();
list.ForEach(Debug.WriteLine);

You should try using the enhanced version of the Where extension method that allows you to filter on item and index. 您应该尝试使用Where扩展方法的增强版本,该方法允许您过滤项目和索引。

Check the reference. 检查参考。 http://msdn.microsoft.com/en-us/library/bb549418.aspx http://msdn.microsoft.com/en-us/library/bb549418.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Verbatim list");
            List<string> list = new List<String> { "A","B","C","D" };

            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }

            Console.WriteLine("Filtered list");
            int itemToSkip = 2;
            foreach (var item in list.Where((item, index) => index != itemToSkip))
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadKey();
        }
    }
}

This will give you the following output. 这将为您提供以下输出。

Verbatim list
A
B
C
D
Filtered list
A
B
D

To skip a position inside the foreach loop, one option is that you can skip the action inside the foreach loop by using an if statement, like 要跳过foreach循环内的一个位置,一个选项是你可以使用if语句跳过foreach循环内的动作,比如

foreach(var item in liste)
{
    if (item != 'X')
    {
        //do something
    }
}

But i am waiting for better solutions 但我在等待更好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在cSharp中检索特定格式的声明? - How to retrieve claims in specific format in c sharp? 如何使用C Sharp调用Excel中的特定单元格? - How to call specific cell in excel with c sharp? 在C#中为每个行集循环在A中跳过一行 - Skip a row in a for each rowset loop in c# c# 用于循环中的每个进度循环 - c# for each progress loop within the loop 如何在cSharp中循环json对象内的json对象 - how to loop the json object inside json object in c sharp 跳过在for循环C#中访问图像像素值的操作 - Skip accessing an image pixel value within a for loop C# C#PDF文档中的尖锐位置 - C# PDF Sharp position in document 您如何 state 中的正则表达式 C# 跳过一个字符,用另一个替换一个字符并在特定的 position 处添加一个新字符? - How do you state a regular expression in C# to skip a character, replace one with another and add a new character at a specific position? 如何在另一个类的另一个实例中遍历一个类的每个离散实例并调用特定方法 - How to loop through each discrete instance of a class within another instance of a different class and call a specific method 如何在 Puppeteer-sharp 中获得鼠标 position? - How to get a mouse position in Puppeteer-sharp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM