问题描述
这是我的代码:
Event thisEvent = (from i in list
where (i.eventID == eventID)
select i).FirstOrDefault();
if (thisEvent != null)
{
thisEvent.eventResolved = resolved;
thisEvent.eventSequence.Add(item);
}
列表"是IEnumerable的集合,即
"list" is a collection of IEnumerable, i.e.
IEnumerable<Event> list;
我想知道的是:使用FirstOrDefault创建thisEvent之后,thisEvent是否仍连接到列表?换句话说,当我更改eventResolved和eventSequence这两个属性时,"list"实际上是否已更改,或者thisEvent只是"list"中某项的完全断开连接的副本?
What I'm wondering is: after creating thisEvent using FirstOrDefault, is thisEvent still connected to list? In other words, when I change the two properties, eventResolved and eventSequence, is "list" actually changed, or is thisEvent just some totally disconnected copy of an item in "list"?
推荐答案
FirstOrDefault
在集合中选择一个项目,但不捕获"或克隆"该项目. IE.它是同一实例.因此,如果您修改属性,则会修改原始实例.
FirstOrDefault
selects an item in a collection, but does not "detatch" or "clone" it. I.e. it is the same instance. So if you modify a property you modify the original instance.
如果要释放"对象,则必须以某种方式将其复制.
If you want to "detatch" the object you will have to copy it in some way or the other.
这篇关于FirstOrDefault之后,对象是否仍连接到列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!