[英]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: 最好使用for循环:
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 . 我在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: 如果您使用的是.NET 3.5和C#3,那么这可以更简单,因此您可以使用扩展方法和隐式类型:
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. 关于使用for
循环的好处是它可以使用IEnumerable<T>
而不是IList<T>
因此您可以将它与LINQ等一起使用而无需缓冲所有内容。 (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循环而不是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): 您可以使用LINQ(如果您使用C#-3.0):
foreach (Item i in items.Take(Items.Count - 1))
{
...
}
As Jon Siegel pointed out: 正如Jon Siegel所指出的那样:
...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. 也就是说,假设你想为IEnumerable<T>
中的每个项目做一些事情,除了一个,其中一个是枚举者任意访问的最后一个。 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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.