C # "downgrade" list?

I have a list of works that I'm trying to work on until all the works have been exhausted, but I don’t know how to deal with it. The idea I'm trying to imitate is this:

foreach(Work w in _workList) { res = DoSomethingOnWork(w); if(res) { _workList.Remove(w); } } 

The reason the work in “DoSomethingForWork (w)” may not be what I want to do at this moment, so I will skip it and move on to the next. When I'm at the end of the list, I want to return to the list. Before I write a collection to handle this, I was curious if there was anything in .NET to handle this situation.

Thanks in advance!

+4
source share
5 answers

This is what the queue class is . It contains a collection, and you simply deactivate each item to make it work. This is the last collection in the past.

If you decide to skip it, you can request it later.

You can also take a look at the stack class , which is similar, but is a collection of last-in, first-out.

+14
source

It is extremely dangerous to modify a collection during its iteration; This is a recipe for crashes.

What would I do in your case is to use the queue, or perhaps to get involved a bit and implement the priority queue. A queue has the property that the last addition is the last to be serviced, just like a queue in a bank; you join the end of the line, and everyone in front of you is first served. So your loop will look something like this:

 while(queue.Count > 0) { var currentCustomer = queue.Dequeue(); bool success = ServiceCustomer(currentCustomer); if (!success) { // the customer couldn't be serviced. Send them to the // back of the line so that we service other customers before // trying again. queue.Enqueue(currentCustomer); } } 

A PriorityQueue works exactly like a queue, except that each task gets the priority associated with it. Tasks with a higher priority become VIPs and are queued before tasks with a lower priority. It's not too difficult to implement your own priority queue.

+7
source

Whenever you are working with deleting items from a list, you cannot make EGUASP, because it calls a numbering device to get a created one that throws an exception when the list is edited ... Use for a loop in the opposite direction:

 for(int i = _workList.Count(); _workList.Count()> 0; i--){ w = _workList[i]; res = DoSomethingOnWork(w); if(res) { _workList.Remove(w); } // Handle reseting i once we've looped through entire list if(i == 0){ i = _workList.Count() - 1; } } 
0
source

If you are using linq and your collection implements IQueryable, you can filter the collection based on this lambda:

 _workList.Where((work) => { return !DoSomethingOnWork(work); }); 

This will return all elements in _workList for which DoSomethingOnWork (work) returns false;

0
source

Another option is for you the Work class to contain a property of type IsCompleted and simply:

 if (!w.IsCompleted) w.IsCompleted = DoSometingForWork(w); 

Very straightforward, readable, easy to maintain, and new programmers should get what he does.

If the loop repeats, you can:

 foreach (Work w in _WorkList.Select(x => !x.IsCompleted)) 
0
source

Source: https://habr.com/ru/post/1379208/


All Articles