Struggling to assign objects to an array in C#.
The output of the code is unexpected because I get the nameOfProject.ClassName(?) x3 rather than name and life points of 3 monsters i added using my for loop. Debug suggests monsters aren't being assigned. Is there a logical flaw that people like me should be looking out for?
class Monsters
{
public Monsters(string name, int points)
{
var monsterName = name;
var monsterLifePoints = points;
}
}
class Program
{
static void Main(string[] args)
{
Monsters[] monster = new Monsters[3];
for (int i = 0; i < monster.Length; i++)
{
Console.WriteLine("Please enter a name");
string name = Console.ReadLine();
Console.WriteLine("Please enter life points");
int points = Convert.ToInt32(Console.ReadLine());
monster[i] = new Monsters(name,points);
Console.Clear();
}
foreach (var element in monster)
{
Console.WriteLine(element);
}
Console.ReadKey();
}
Monsters, so that's what you get.