Steffon Scott

Steffon Scott

  • NA
  • 20
  • 12.4k

Inefficiencies of iterating over a custom list to obtain reference.

Sep 23 2012 9:31 PM
Hello again. Correct me if I am wrong for posting code here, I was looking for posting guidelines but couldn't see any on the main page or the posting page, perhaps I am overlooking it. I am requesting a possibly different algorithm for a function I am performing in my code:

public void showStats(string player)
        {
            foreach (playerData playerstats in Program.playerList)
            {
                if (playerstats._playerName == player)
                {
                    Console.WriteLine();
                    Console.WriteLine("Player Name: {0}", playerstats._playerName);
                    Console.WriteLine("Player ID: {0}", playerstats._playerID);
                    Console.WriteLine("Player Level: {0}", playerstats._playerLevel);
                    Console.WriteLine("Player Land: {0}", playerstats._playerLand);
                    Console.WriteLine("Player Peasants: {0}", playerstats._playerPeasants);
                    Console.WriteLine("Player Moves: {0}", playerstats._playerMoves);
                    Console.WriteLine("Player Copper: {0}", playerstats._playerCopper);
                    Console.WriteLine("Player Castles: {0}", playerstats._playerCastles);
                    Console.WriteLine();
                }
            }
        }

Now, just one look at this code makes me cringe because it doesn't seem at all efficient in any sense of the word, to iterate over a list to obtain the correct reference to the object I wish to display but this is how I got it to work. Is there a better way of doing this?