Question Description
This C# code displays the player characters’ inventory in the user interface of an RPG game. The functions DisplayHeader and DisplayItems must be called in order for each party member. DisplayItems takes the type List<string> as a parameter and cannot be modified.
for (int partyMember = 0; partyMember < 5; ++partyMember) {
var partyItems = new List<string>();
for (int item = 0; item < 10; ++item) {
var itemName = Party[partyMember].Items[item].Name;
partyItems.Add(itemName);
}
DisplayHeader($"Items for member {partyMember}:");
DisplayItems(partyItems);
}
Profiling the game reveals this function is causing performance problem because it allocates too much memory. Where is this code allocating memory, and what changes could you make to reduce the amount of memory allocated?