Marek Jarusz
Marek Jarusz

Reputation: 21

Getting only last value from List<T>

I have problem with my list because I always get only last value from this list. I don't know how can I fix it... I've tried with override etc but it's doesnt' function or I did this incorrect.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp13
{
    class Program
    {
        static public List<Person> personList = new List<Person>();
        static void Main(string[] args)
        {

            Person person = new Person();
            do
            {
                Console.WriteLine("Your name: ");
                person.Name = Console.ReadLine();
                Console.WriteLine("Your Surname: ");
                person.Surname = Console.ReadLine();
                personList.Add(person);
            }
            while (personList.Count < 3);

            foreach (Person item in personList) 
            {
                Console.WriteLine(item.Name + " " + item.Surname);
            }


            Console.ReadKey();
        }
    }

    class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

}

Upvotes: 1

Views: 90

Answers (3)

Tiago Silva
Tiago Silva

Reputation: 223

  Person person = new Person();
            do
            {
                Console.WriteLine("Your name: ");
                person.Name = Console.ReadLine();
                Console.WriteLine("Your Surname: ");
                person.Surname = Console.ReadLine();
                personList.Add(person);

When you add a person to personList, it is a reference to person object. It is not a copy of that object. It implies that if You want different objects, You also need to create a new Person Object in each interaction.

do
            {
                Person person = new Person();
                Console.WriteLine("Your name: ");
                person.Name = Console.ReadLine();
                Console.WriteLine("Your Surname: ");
                person.Surname = Console.ReadLine();
                personList.Add(person);

Take a look at references and value type. https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type

Upvotes: 0

Krishna Varma
Krishna Varma

Reputation: 4250

Initialize the person object within loop

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp13
    {
        class Program
        {
            static public List<Person> personList = new List<Person>();
            static void Main(string[] args)
            {
    
                
                do
                {
                    Person person = new Person();//initialize here
                    Console.WriteLine("Your name: ");
                    person.Name = Console.ReadLine();
                    Console.WriteLine("Your Surname: ");
                    person.Surname = Console.ReadLine();
                    personList.Add(person);
                }
                while (personList.Count < 3);
    
                foreach (Person item in personList) 
                {
                    Console.WriteLine(item.Name + " " + item.Surname);
                }
    
    
                Console.ReadKey();
            }
        }
    
        class Person
        {
            public string Name { get; set; }
            public string Surname { get; set; }
        }
    
    }

Upvotes: 0

Mitul&#225;t b&#225;ti
Mitul&#225;t b&#225;ti

Reputation: 2166

Because you're modifying the same instance 3 times. Put the instantiation of the person inside the do while loop like this:

...
do
{
    var person = new Person();
    Console.WriteLine("Your name: ");
...

Upvotes: 3

Related Questions