当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - List of value type: Changes on member of object from list are not saved in list

问题描述:

CLocation is a C++CLI value class (would be a struct in C#) with public members x, y, z.

List<CLocation>^ listloc = gcnew List<CLocation>;

listloc->Add (CLocation (1,2,3));

listloc[0].x = 5; // FAILS, '5' is not stored.

array<CLocation>^ aloc = gcnew array<CLocation>(1);

aloc[0] = CLocation (1,2,3);

aloc[0].x = 5; // WORKS

Why is 5 not saved in the member of the object in the list, while it is perfectly saved in the member of the object of the array?

Does list access return a copy of the object as Marc Gravell wrote in In C#, are the values in a List<struct> boxed? ?

分享给朋友:
您可能感兴趣的文章:
随机阅读: