C# List.ForEach(delegate(type t) and how to remove items as the list is iterated through -


This can be considered bad programming, but before .net 4, I used to make heavy use of code like this: / P>

  enemyList.ForEach (representative (enemy e) (e. Update), if (some condition) enemy list. Remove (e);});   

Now, I am going through updating some old projects, and there are lot of the code that were later deleted because they Must have changed. Now, I have an extension for which I have permission to use the foray:

  public static void ForEach & lt; T & gt; (This IEnumerable & lt; T & gt; sequence, action & lt; T & gt; action) {if (sequence == faucet) remove new logic ("sequence"); If (action == empty) remove new logic ("action"); Forchach (T items in sequence) verb (object); }   

I know I can do this:

  var index = 0; Whereas (index & lt; enemyList.Count) {if (condition) enemyList.RemoveAt (index); Other index ++; }   

But some of them would be a pain to write in this way. Is there any way to add that functionality back, so that I can iterate over that list, go back and write and edit all those functions? I still consider myself a newbie for coding, and I can not understand it once. Any help would be appreciated!

** * ** * *** edit < Strong> ** * ** * ** *

So I suppose That is it proves to be able to rewrite a lot of code. I have a lot of code I is out of a project:

  gameplaycryin gameWorld.shipList.ForEach (representative (ship) { If (eS.originalShipsID == s.shipID) {if (! ES.Alive || eS .health & lt; = 0) {// This is a sinking ... string log = "0" + s.shipName + " All the crew and cargo were lost. "AddLogEntry (log); totalCrewLost + = S.current Crew; GameplayScreen.gameWorld.shipList.Remove (S);}}});   

I was just hoping that not all of them have to be rewritten. That's why I give the time to update and change the code apparently. Thanks!

Use the remove method of the list.

You can re-apply the code to:

  enemyList.RemoveAll (enemy => enemies; some condition);   

Not only this while is better than loop, I argue that it is much better than the foreach method.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -