Click here to Skip to main content
14,336,321 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have been trying to print the contents of a list of records into the console in Visual Studio 2017, but i can't get it to print the list's contents, just the list's position in my code (as i understand it).

Here is the code:

using System;
using System.Collections.Generic;



namespace list_of_records
{
    class Program
    {
        static void Main(string[] args)
        {
            List<StudentData> students = new List<StudentData>();

            StudentData student = new StudentData();
            
            for (int i = 0; i <= 1; i++ )
            {
                Console.WriteLine("first name: ");
                student.firstName = Console.ReadLine();
                Console.WriteLine("last name: ");
                student.lastName = Console.ReadLine();
                Console.WriteLine("age: ");
                student.age = Convert.ToInt32(Console.ReadLine());


                students.Add(student);
            }


            //students.ForEach(Console.WriteLine);
            /*
            foreach (var st in students)
            {
                Console.WriteLine(st);
            }
            */
            for (int i = 0; i < students.Count; i++)
            {
                Console.WriteLine(students[i]);
            }

        }



        public class StudentData
        {
            public string firstName;
            public string lastName;
            public int age;
        }

    }
}


it is printing:
list_of_records.Program+StudentData
list_of_records.Program+StudentData


Which is as i understand it the position of the list in my project.

why does it do this and how can i make it do my bidding?

Thanks.

What I have tried:

you can see what i have tried in the code, and some other methods of printing that found on the internet, but to no avail.
Posted
Updated 11-May-19 19:59pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

That's because your StudentData class doesn't implement ToString - so the default version for object is used instead, and that prints the full name of the class (because it doesn't "know" about the content you added and what you want printed).
Try this:
public class StudentData
    {
    public string firstName;
    public string lastName;
    public int age;
    public override string ToString()
        {
        return $"{firstName} {lastName} is {age} years old";
        }
    }
Or better, this:
public class StudentData
    {
    public string firstName { get; set; }
    public string lastName { get; set; }
    public int age { get; set; }
    public override string ToString()
        {
        return $"{firstName} {lastName} is {age} years old";
        }
    }
Because you shouldn't expose fields directly - use properties instead.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100