C
C#2y ago
DiMiaN

✅ Outputting values from a list

When typing in name and age it doesnt display the correct values. Instead it outputs: Program+Person.
internal class Program
{
public class Person
{
public string name { get; }
public int age { get; set; }
public int weight { get; set; }
public int height { get; set; }

public Person(string name, int age)
{
this.age = age;
this.name = name;
this.weight = 0;
this.height = 0;
}

// Rest of the Person
}

private static void Main(string[] args)
{
List<Person> persons = new List<Person>();

// Read the names of persons from the user
while (true)
{
Console.Write("Enter a name, empty will stop: ");
String name = Console.ReadLine();
if (name == "")
{
break;
}

Console.Write("Enter the age of the person " + name + ": ");

int age = Convert.ToInt32(Console.ReadLine());

// Add to the list a new person
// whose name is the previous user input
persons.Add(new Person(name, age));
}

// Print the number of the entered persons, and their individual information
Console.WriteLine();
Console.WriteLine("Persons in total: " + persons.Count);
Console.WriteLine("Persons: ");

foreach (Person person in persons)
{
Console.WriteLine(person);
}
}
}
internal class Program
{
public class Person
{
public string name { get; }
public int age { get; set; }
public int weight { get; set; }
public int height { get; set; }

public Person(string name, int age)
{
this.age = age;
this.name = name;
this.weight = 0;
this.height = 0;
}

// Rest of the Person
}

private static void Main(string[] args)
{
List<Person> persons = new List<Person>();

// Read the names of persons from the user
while (true)
{
Console.Write("Enter a name, empty will stop: ");
String name = Console.ReadLine();
if (name == "")
{
break;
}

Console.Write("Enter the age of the person " + name + ": ");

int age = Convert.ToInt32(Console.ReadLine());

// Add to the list a new person
// whose name is the previous user input
persons.Add(new Person(name, age));
}

// Print the number of the entered persons, and their individual information
Console.WriteLine();
Console.WriteLine("Persons in total: " + persons.Count);
Console.WriteLine("Persons: ");

foreach (Person person in persons)
{
Console.WriteLine(person);
}
}
}
10 Replies
TheRanger
TheRanger2y ago
please post code with $code instead
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
TheRanger
TheRanger2y ago
$codegif
Pobiega
Pobiega2y ago
Console.WriteLine(person); this will call ToString on person and the default tostring for an object is to print the class name you need to override ToString in your Person class
DiMiaN
DiMiaNOP2y ago
oh okay? I was following a simple tutorial and this is one of the examples
Pobiega
Pobiega2y ago
well either you do something like...
Console.WriteLine($"{person.Name}: {person.Age}");
Console.WriteLine($"{person.Name}: {person.Age}");
or you override the ToString method in your class
DiMiaN
DiMiaNOP2y ago
thanks!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
DiMiaN
DiMiaNOP2y ago
/close
Want results from more Discord servers?
Add your server