-1

I would like to add (within a loop) class instances to a list but on each iteration I only want to update one field of the class. Here is my illustation example:

        public class info
    {
        public string aninmalorhuman;
        public string type;
    }

        var temp = new info();
        var infolist = new List<info>();
        temp.aninmalorhuman = "animal";
        temp.type = "dog";
        infolist.Add(temp);
        temp.type = "cat";
        infolist.Add(temp);

If I give infolist to the console it prints "animal cat" & "animal cat". I guess first for my learning why??? and second how do I make it print "animal dog" & "animal cat" please?

UPDATE

I see how this was not the smartest question but I had beginners confusion so I do not really see why this is voted down. Anyways, thank you to those who responded for clearing this up.

nik
  • 1,672
  • 2
  • 17
  • 36

4 Answers4

2

This is because class is a reference type. So basically you are modifying same piece of memory on the heap.Good explanation of stack/heap In other words, in your code you have 1 pointer on the stack, which is pointing to the same piece of memory. (at the beginning you have "dog" on the heap, and then you are replacing "dog" with a "cat").

If you want to insert new object then you need to create new isntance of "Info Class"

Example:

            var temp = new info();
            var infolist = new List<info>();
            temp.aninmalorhuman = "animal";
            temp.type = "dog";
            infolist.Add(temp);
            temp = new info();
            temp.aninmalorhuman = "animal";
            temp.type = "cat";
            infolist.Add(temp);
Community
  • 1
  • 1
Jack
  • 350
  • 2
  • 15
1

You add two times the same object (same reference to the object).

When you change the value of the property 'type', you change the one which is contained in the list too.

Béranger
  • 671
  • 8
  • 23
1

you're working with the same object all the time. Create a new object and add it to your list

List<info> infolist = new List<info>();

info dog = new info(); //info object for the dog
dog.aninmalorhuman = "animal";
dog.type = "dog";
infolist.Add(dog);

info cat = new info(); //info object for the cat
cat.aninmalorhuman = "animal";
cat.type = "cat";
infolist.Add(cat);
fubo
  • 44,811
  • 17
  • 103
  • 137
0

Change class to struct:

public struct info
{
    public string aninmalorhuman;
    public string type;
}

If you want to use classes, implement ICloneable interface:

public class info : ICloneable
{
    public string aninmalorhuman;
    public string type;

    public info Clone()
    {
        return new info()
        {
            aninmalorhuman = this.aninmalorhuman,
            type = this.type,
        };
    }

    object ICloneable.Clone()
    {
        return Clone();
    }
}

var temp = new info();
var infolist = new List<info>();
temp.aninmalorhuman = "animal";
temp.type = "dog";
infolist.Add(temp);
temp = temp.Clone();
temp.type = "cat";
infolist.Add(temp);
General-Doomer
  • 2,681
  • 13
  • 13