2

I've recently run into a snag in my program where using a list of a user defined class has been corrupting data. Example:

class myClass {
    public int x;
}

myList<myClass> = new List<myClass>();
myClass myObject = new myClass();

myList.Add(myObject);
myList.Remove(myObject);

When I try to add something to the list, my data is being corrupted. I believe removing objects is also causing problems, although this could be because they're corrupted in the first place. I've seen similar issues here and there and was hoping someone could explain to me what's going on. While the links do provide me with some answers, they're not doing a good job of explaining what's going on behind the scenes.

EDIT: I should have been more clear before about how my data is being corrupted. The values in the fields of my objects are being changed when they are added to the list. For the purposes of my program, the values should all be 0 or 1 but when added to the list change to -1 to 3.

EDIT 2: The source of the problem was an unfamiliarity with C#. Coming from a C++ background I assumed that objects were passed by value and not by reference. Changing my custom class from a class to a struct resolved the issue instantly. Thanks to @MattW !

Community
  • 1
  • 1
Max
  • 261
  • 1
  • 4
  • 9
  • 3
    In what way is the data getting corrupted? – Fredrik Mörk Sep 14 '12 at 06:19
  • From the link it looks like you are trying to check `Contains` against the list and you are not getting the desired result ? if that is the case please add it in your question, `data getting corrupted` is not telling us what is the problem you are having – Habib Sep 14 '12 at 06:27
  • is your data corrupted or changed ?? – Anant Dabhi Sep 14 '12 at 06:41
  • 1
    Are you attempting to enumerate (foreach) over the collection while you are adding or removing items? – Tormod Sep 14 '12 at 06:43
  • When does it get corrupted/changed? If you immediately check the value after you add it is it already changed? – MattW Sep 14 '12 at 17:44
  • It seems I can add it to the list and the data is fine but as soon as I check it the value has changed. I'm passing this list as an argument to another function. Could that be causing issues? – Max Sep 14 '12 at 20:30
  • @Tormod I'm using a foreach on objects that contain these lists so yes. – Max Sep 15 '12 at 01:00
  • 1
    @Max I meant to ask if you were modifying the list while iterating over it. Ie, modifying it INSIDE the loop. I see that you have found the cause of the problem: that you were actually just re-adding the same object. Or more precisely: you were adding multiple references to the same object. – Tormod Sep 15 '12 at 05:45
  • @Tormod Exactly. I kept instantiating objects with the same variable (which in C++ would have made a copy of the variable for each instantiation) and then modifying that variable and instantiating more objects with it. – Max Sep 15 '12 at 14:14

1 Answers1

5

There isn't too much to go on here - so the following is a guess at the problem ...

Are you changing the value of myObject after adding it to the List<>? Remember that since you are adding a Class to the list (and all Classes are Reference types), that if you change the value of myObject after adding it to the list, it will be changed everywhere (including inside of your List<>).

For example:

List<myClass> myList = new List<myClass>();
myClass myObject = new myClass();
myObject.x = 5;

myList.Add(myObject);
Console.WriteLine(myList[0].x); //this will be 5

myObject.x = 7;
Console.WriteLine(myList[0].x); //this will be 7

Even though you didn't touch the List<> itself, you changed the value of the Referenced object, so it is changed everywhere it is being Referenced.

Check Out Value vs Reference types

Ryan Dantzler
  • 1,124
  • 1
  • 8
  • 18
MattW
  • 12,902
  • 5
  • 38
  • 65
  • I should have read this a little closer. I'm coming from the world of C++ where this would have been passed in an argument as a value and not a reference. Looks like I need to do a little reading on the differences between C# and C/C++. Thanks a lot! – Max Sep 15 '12 at 01:28
  • I don't know if I should delete this post or leave it up in case someone else makes the same mistake. I can't imagine this post will be targeting the right audience in its current state. – Max Sep 15 '12 at 01:30