fatima Ajij
fatima Ajij

Reputation: 192

How to display returned List in c#

I want to display returned list to other class. I am returning the list from the method by passing an object of the auto property class, but can't display it... here is the code.

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
     }

 public List<Person> LoadData() {

            List<Person> people = new List<Person>();

            people.Add(new Person { FirstName = "Sara", LastName = "John", Age = 30 });
            people.Add(new Person { FirstName = "Tim", LastName = "Joe", Age = 26 });
            people.Add(new Person { FirstName = "James", LastName = "ted", Age = 29 });

            return people;

        }

//The other side where I want to display the code is

class Program
    {
        static void Main(string[] args)
        {
            var display = new Person();
            var p = new List<Person>();

            p = display.LoadData();

            foreach (var item in p)
            {
                Console.WriteLine(p);
            }
        }
    }

I am not getting any Error but it's not displaying data, It displays

System.Generice.Collections.MyConsoleApplication.

Upvotes: 1

Views: 3731

Answers (4)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34180

Person is not a string and Console.WriteLine(p); won't result in what you want.

So there are two ways to go:

Either write it like this:

foreach (var item in p)
{
    Console.WriteLine(string.Format("{0} {1}, {2}", item.FirstName, item.LastName, item.Age));
}

Or just override the ToString() Method of your Person class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return string.Format("{0} {1}, {2}", FirstName, LastName, Age);
    }
}

in this case (second option) your own code will work:

foreach (var item in p)
{
     Console.WriteLine(item);
}

Upvotes: 4

Mike
Mike

Reputation: 3460

Others have already mentioned the Object.ToString problem. One other issue in your code is that the for loop is trying to print the collection not the items

foreach (var item in p) { Console.WriteLine(p); }

should be using item not p

foreach (var item in p) { Console.WriteLine(item.ToString()); }

Upvotes: 1

Terry Tyson
Terry Tyson

Reputation: 623

Try using:

Console.WriteLine(item.FirstName);

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30665

tour Person class does not know how to print string. you need to override ToString()

public class Person
{
    public override string ToString()
    {
        return $"FirstName = {this.Firstname} \t\t LastName = {this.LastName} \t\t Age = {this.Age}";
    }
}

now your foreach will print by using override function

foreach (var item in p)
{
     Console.WriteLine(p);
}

Upvotes: 0

Related Questions