I encountered several problems when creating storing objects to an Array and printing in c# after debugging. What is my problem here? The problem started occurring at adding the objects to the Array and printing the object title.
static void Main(string[] args)
{
ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
Console.WriteLine(cg1.title);
ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
ComputerGame[] gameAlbum = new ComputerGame[5];
for (int i = 0; i < 5;i++)
{
gameAlbum[0] = new ComputerGame();
gameAlbum[1] = new ComputerGame();
gameAlbum[2] = new ComputerGame();
}
foreach(ComputerGame o in gameAlbum)
{
Console.WriteLine(o.title);
}
}
public class ComputerGame
{
public string title;
public double price;
public ComputerGame(string title, double price)
{
this.title = title;
this.price = price;
}
}