C# newbie working off some tutorials. Is there a way to run an identical command on every object in an array?
Here is my current (working) code:
Guys[0].Cash += Guys[0].MyGuess.PayOut(WinnerNumber);
Guys[1].Cash += Guys[1].MyGuess.PayOut(WinnerNumber);
Guys[2].Cash += Guys[2].MyGuess.PayOut(WinnerNumber);
I’m looking for something that will do this:
Guys[X].Cash += Guys[X].MyGuess.PayOut(WinnerNumber);
X = is first number on first runthrough, then 2nd number on 2nd runthrough, etc.
You have several options:
Plain old
forloop:foreachblock:Array.ForEach:These, for me, are in order of preference. Most will prefer the
forloop because that is the familiar way of doing something to every item in array in sequence. It’s close though betweenforandforeach.