【发布时间】:2018-10-04 04:06:50
【问题描述】:
假设我有一本 Something 的字典:
private Dictionary<uint, Something> _somethingList = new Dictionary<uint, Something>();
Something 是,例如:
class Something
{
public uint Id { get; set; }
public string Name { get; set; }
// ... more stuff here
}
然后我有Another 类:
class Another
{
public Another(uint id, string name, Something connection)
{
Id = id;
Name = name;
Connection = connection;
}
public uint Id { get; set; }
public string Name { get; set; }
public Something Connection { get; set; }
}
并像这样使用它:
var test = new Another(1, "Test", _somethingList[1]);
Connection 将是 _somethingList 字典中键为 1 的值的引用或副本,还是会为其创建新内存?
我想我要问的是,它在内存中的表现如何,例如,它是否会复制内存中的属性数据,还是会像指向实际 _somethingList[1] 的指针一样?或者我如何自己验证这一点(我的意思是,如果我断点,是否有东西表明它是副本还是参考还是 w/e)?
【问题讨论】:
-
这将是参考,不会创建新的内存。如果您将在其他地方更改
_somethingList[1]的成员。它也会在test对象的Connection中更改。 -
@Amit 感谢您的评论,所以
Connection就像是指向字典实际值的指针? -
是的。 Connection 只会携带
_somethingList[1]的引用。它不会保存实际数据。 -
@Amit 好的,谢谢,我担心它可能是在复制它而不是引用它,我想知道是否有办法通过中断指向它来识别它
-
@Guapo 在 Visual Studio 中,右键单击本地窗口并选择 "Make Object ID",它将为特定对象分配一个唯一的整数。然后你可以区分两个引用是否指向完全相同的对象。
标签: c# pointers memory reference copy