Weird value referencing

▼魔方 西西 提交于 2019-12-14 02:36:24

问题


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)


回答1:


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




回答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.




回答3:


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.




回答4:


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#




回答5:


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.




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/8148385/weird-value-referencing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!