1

I faced a problem today which has happened to me several times before. I have a generic list of my object:

List<Classes.Object.GameObject> ObjectList = new List<Classes.Object.GameObject>();

I decided to get an instance of Classes.Object.GameObject from the list by:

Classes.Object.GameObject TempObject = new Classes.Object.GameObject;
TempObject = ObjectList[10];

It's working good till here but when I do some changes on TempObject the object of index in list which I mentioned changes too! I mean the ObjectList[10] will change by changing of TempObject. There is no referencing at all. Maybe I didn't understand OO programming well.

Dear moderators you made a mistake in editing my grammar! (you changed till to until)

MahanGM
  • 2,352
  • 5
  • 32
  • 45

7 Answers7

2

In C# all objects are handled through references. The problem is here:

// Create a new GameObject and have the TempObject reference point to it.
Classes.Object.GameObject TempObject = new Classes.Object.GameObject;
// Change the TempObject reference to point to the object at ObjectList[10].
TempObject = ObjectList[10];

TempObject now refers to the object that is within the list. If you change it, you will change the object in the list - because they are the same object. In C# you have to explicitly create a new object from the old one if you want a separate object. If a shallow copy is enough, you can use the MemberWiseClone method to create a new object.

If you are coming from C++ this is a common pitfall; in C++ this would have copied the object at ObjectList[10] into TempObject.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
1

Because you are assigning a reference of GameObject to the TempObject. Both ObjectList[10] and TempObject reference varibles have the reference of a common object.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

If you want to change TempObject without impact on its "original version" on list do a copy. Described in Cloning objects in C#.

Community
  • 1
  • 1
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
0

Make sure in this case create a deep copy of the object if you dont want it to be referenced.

Use this link for more info Deep Copy in C#

Community
  • 1
  • 1
Ashley John
  • 2,379
  • 2
  • 21
  • 36
0

With every Reference Type (lets say every "class") you only set the reference to variable by using the "=" Imagine your list as a list of references. When assigning ObjectList[10] to your TempObject, you will reference the same actual object as ObjectList references.

If you want to copy of an object make yourself a Copy() Method.

Jobo
  • 1,084
  • 7
  • 14
0

What about defining Classes.Object.GameObject as a value type (struct) instead of a reference type (class)? Can you show the definition of GameObject?

Michal B.
  • 5,676
  • 6
  • 42
  • 70
0

Both TempObject and ObjectList[10] are pointing to same memory location in the heap as they are objects of classes and hence Reference types. So, if you change the value in one object variable it gets reflected in other variable also due to changes made at one memory location.

You can get a good grasp of memory allocation in value and reference types in the below article:

http://www.albahari.com/valuevsreftypes.aspx

S2S2
  • 8,322
  • 5
  • 37
  • 65