c# - Clearing a list using a for loop -


I'm making a black jack game, and at the beginning of every new round I need to clear the list of cards And represents the dealer's hands. I used to do this:

  Public Zero ClearPlayerHand () {for (int i = 0; i   

The problem seems to always be left with a card left in the list, or I get out of bound error, regardless of how I change the value of Is the best way to remove all elements from the player?

If your archive implements PlayerHand you can only call You can. Clear () method.

This is a general implementation of the interface.

If you want to clear one list & lt; T & gt; For for loop, you should use a reverse for the loop, the reason for this is that when you remove an item from the list , Then it will make a change under all the indices, and you can easily run into the index from the exceptions.

An example would be:

 for  (int i = player hack.count - 1; i> = 0; i--) {PlayerHand.RemoveAt (I); }    

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 -