如果我在 foreach 循环中使用一个项目并且我不能使用该项目,它必须删除当前在 foreach 循环中的项目。
这是我现在拥有的代码:
foreach (Line line in linelijst)
{
try
{
if (line.ActorIndex() == 0)
{
line.setStartPoint(actorenlijst[0].getLinePoint()); //if actorenlijst[0] doesn't excist it has to delete the current line
}
if (line.ActorIndex() == 1)
{
line.setStartPoint(actorenlijst[1].getLinePoint()); //if actorenlijst[1] doesn't excist it has to delete the current line
}
if (line.ActorIndex() == 2)
{
line.setStartPoint(actorenlijst[2].getLinePoint()); //if actorenlijst[2] doesn't excist it has to delete the current line
}
Point start = line.getStartPoint();
Point end = line.getEndPoint();
Pen lijn = new Pen(Color.Black, 1);
graphics.DrawLine(lijn, start, end);
}
catch
{
//delete current line from the list
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您有兴趣帮助其他人:)
小智 8
尝试为需要删除的项目创建另一个临时列表,然后当您完成循环时,您可以删除临时列表中的项目。
List<Type> temp = new List<Type>()
foreach(item in mainList)
{
if (item.Delete)
{
temp.Add(item);
}
}
foreach (var item in temp)
{
mainList.Remove(item);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9811 次 |
| 最近记录: |