The coach corrects a particular recorded score. Use brackets to read or replace the item at a known current position.
Use using System.Collections.Generic; at the top of the C# file, before its class definitions. Place the following statements inside an existing method.
List<int> roundGoals = new List<int>();
roundGoals.Add(2);
roundGoals.Add(0);
roundGoals.Add(3);
int secondRoundGoals = roundGoals[1];
roundGoals[1] = 1;The read copies 0 into secondRoundGoals. The assignment then replaces the list item at index 1 with 1. secondRoundGoals remains 0 because it holds the earlier copied integer value. The list now holds 2, 1, 3, and Count is still 3.
int requestedIndex = 3;
bool hasScore = false;
int selectedGoals = 0;
if (requestedIndex >= 0 && requestedIndex < roundGoals.Count)
{
selectedGoals = roundGoals[requestedIndex];
hasScore = true;
}Both tests must be true. Valid indexes run from 0 through Count - 1, so requestedIndex 3 is rejected for this three-item list. Count itself is not a readable position.