C# 在上一次迭代中,如何逃逸foreach循环?

C# 在上一次迭代中,如何逃逸foreach循环?,c#,list,C#,List,最好使用for循环: foreach (Item i in Items) { do something with i; do another thing with i (but not if last item in collection); } int itemCount=Items.Count; 对于(int i=0;i

最好使用for循环:

foreach (Item i in Items)

 {
      do something with i;
      do another thing with i (but not if last item in collection);
 }
int itemCount=Items.Count;
对于(int i=0;i
用for循环代替foreach怎么样

int itemCount = Items.Count;
for (int i = 0; i < itemCount; i++)
{
    var item = Items[i];

    // do something with item

    if (i != itemCount - 1)
    {
        // do another thing with item
    } 
}
for(int i=0;i
我有一个问题。示例代码(来自第一个链接):

foreach(SmartEnumerable.Entry in
新智能数字(列表))
{
Console.WriteLine(“{0,-7}{1}({2}{3}”),
entry.IsLast?“Last->”:“,
条目。值,
条目索引,
entry.IsFirst?”:“”,
条目。值,
条目索引,
entry.IsFirst?”您可以使用LINQ(如果您使用C#-3.0):


正如Jon Siegel指出的那样:


……当然是最后一个概念 如果 集合未编制索引

这就是说,假设您希望对
IEnumerable
中的每个项目执行某些操作,但其中一项除外,该项是枚举器任意访问的最后一项。很好:

    foreach (Item i in items.Take(Items.Count - 1))
    {
...
    }
IEnumerator e=Items.GetEnumerator();
e、 MoveNext();
while(e.Current!=null)
{
第i项=电流;
//和我做点什么;
如果e.MoveNext()
{
//和我一起做另一件事
}
}

写这篇文章我会觉得很肮脏,但它可能会解决你的问题

IEnumerator<Item> e = Items.GetEnumerator();
e.MoveNext();

while (e.Current != null)
{
    Item i = e.Current;
    // do something with i;

    if e.MoveNext()
    {
        // do another thing with i
    }
}

看起来这就是你想要解决的问题

Item last = null;

foreach (Item i in Items)
{
      last = i;
}

foreach (Item i in Items)
 {
      do something with i;

      if (i!=last){
            do another thing with i (but not if last item in collection);
      }
 }
List List=getList();
Join(“,”,list.ToArray());

您可以按照按钮单击事件中的逻辑操作

List<string> list = getList();
string.Join(", ", list.ToArray());
namespace LastEnumItem
{
公共部分类Form1:Form
{
List lst=新列表();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{

对于(int i=0;i+1,尽管这仅在Items集合被索引时有效……当然,如果集合未被索引,则集合中最后一项的概念毫无意义。@Jon:一点也不。
IEnumerable
本身是有序的,但没有索引。(或者更准确地说,它本质上是有序的,但没有索引的随机访问,也不提供预先计数。)谢谢你的回答,但是我必须把它交给jon skeet,因为他的解决方案仍然使用foreach,这是有效的。这不起作用,因为它不会对最后一项执行第一个操作。是的,第一个操作应该在最后一项的循环之后重复。这很好。以前看过你的MiscUtil库,但有点过头了我肯定有很多这样的好东西。如果我有时间去看看的话。:“通常是为了在项目之间输出某种分隔符或类似的东西。”"-正是我的问题。是的,这有点像一个抓包。在某个时候,我应该把它分离出来,或者至少把它移到code.google.com上。这只是一个寻找时间的例子……从好的方面来说,我实现它的方式,你通常可以抓取一两个类,而不是整个库。@zsharp,你真的应该提到y您正在尝试这样做:您可以使用string.Join()来完成此操作!这不是最好的方法:在这里,您可以在Items集合中再循环一次,如果Item在Equals方法中有一些复杂的逻辑,这将影响性能(这一行:“if(i!=last){”)。@Kamarey:这是一个很好的解释,说明了为什么您应该“写这篇文章感觉很脏”。有时你可以把这个问题说得更容易处理。这是我写的一篇关于上次有人问我这个问题的文章:谢谢你写的这篇好文章,但尽管我不是一名高级程序员,但我仍然想知道如何确定最后一项,而不考虑目前的实际目的。我认为这个问题很清楚比我的任何一个混为一谈的人都要多。
    foreach (Item i in items.Take(Items.Count - 1))
    {
...
    }
IEnumerator<Item> e = Items.GetEnumerator();
e.MoveNext();

while (e.Current != null)
{
    Item i = e.Current;
    // do something with i;

    if e.MoveNext()
    {
        // do another thing with i
    }
}
Item last = null;

foreach (Item i in Items)
{
      last = i;
}

foreach (Item i in Items)
 {
      do something with i;

      if (i!=last){
            do another thing with i (but not if last item in collection);
      }
 }
List<string> list = getList();
string.Join(", ", list.ToArray());
namespace LastEnumItem
{
    public partial class Form1 : Form
    {
        List<string> lst = new List<string>(); 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for(int i=0; i<=10 ; i++)
            {
                lst.Add("string " + i.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string lastitem = lst[lst.Count-1];
            foreach (string str in lst)
            {
                if (lastitem != str)
                {
                    // do something
                }
                else
                {
                    MessageBox.Show("Last Item :" + str);
                }
            }
        }


    }
}