Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

c# List<int> in class

Name: tt 2011-03-05 12:18

So i made a class and put a public List<int> in there. Seems like every element in that class is using the same list. Even though i have new list() in the constructor.

Can anybody help?

Name: Anonymous 2011-03-05 12:49

>>22
In C++, you could overload the assignment operator (which is almost never a good idea). In C#, you cannot. The closet you can get is to add a constructor public notacija(notacija other) that initializes its own fields from other's. For value fields, you should simply do x.field=y.field;. For reference fields, you are going to have to create a new object of the same type, and copy over the values inside it manually.

The resulting code should look like:

MyClass X = new MyClass(1,3,4);
MyClass Y;

Y = new MyClass(X);

Now, X and Y should be completely independent and should share no references in their fields.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List