简体   繁体   中英

How do I escape a foreach loop during last iteration?

foreach (Item i in Items)

 {
      do something with i;
      do another thing with i (but not if last item in collection);
 }

Better to use a for loop:

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
    } 
}

I have a helper class for this in MiscUtil . Sample code (from the first link):

foreach (SmartEnumerable<string>.Entry entry in
         new SmartEnumerable<string>(list))
{
    Console.WriteLine ("{0,-7} {1} ({2}) {3}",
                       entry.IsLast  ? "Last ->" : "",
                       entry.Value,
                       entry.Index,
                       entry.IsFirst ? "<- First" : "");
}

This is simpler if you're using .NET 3.5 and C# 3 so you can use extension methods and implicit typing:

foreach (var entry in list.AsSmartEnumerable())
{
    Console.WriteLine ("{0,-7} {1} ({2}) {3}",
                       entry.IsLast  ? "Last ->" : "",
                       entry.Value,
                       entry.Index,
                       entry.IsFirst ? "<- First" : "");
}

The nice thing about this over using a for loop is that it works with IEnumerable<T> instead of IList<T> so you can use it with LINQ etc without buffering everything. (It maintains a single-entry buffer internally, mind you.)

foreach (Item i in Items.Take(Items.Count - 1))
{
      do something with i;
      do another thing with i (but not if last item in collection);
}

How about a for loop instead of a foreach.

for (int i = 0; i < Items.Count; i++) {
    //do something with i;
    if (i == Items.Count - 1) {
        //do another thing with Items[Items.count - 1];
    }
}

You could use LINQ (if you use C#-3.0):

    foreach (Item i in items.Take(Items.Count - 1))
    {
...
    }

As Jon Siegel pointed out:

...of course the notion of the last item in a collection is meaningless if the collection isn't indexed.

That said, suppose you want to do something for every item in an IEnumerable<T> except for one, that one being whichever happens to be the last one arbitrarily visited by an enumerator. Fine:

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
    }
}

looks like this is what you were trying to solve.

List<string> list = getList();
string.Join(", ", list.ToArray());

i'd feel pretty dirty writing this but it might solve your problem.

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);
      }
 }

You can follow this logic as in button click event..

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);
                }
            }
        }


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM