Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an object still connected to a list after FirstOrDefault?

Here's my code:

        Event thisEvent = (from i in list
                           where (i.eventID == eventID)
                           select i).FirstOrDefault();
        if (thisEvent != null)
        {
            thisEvent.eventResolved = resolved;
            thisEvent.eventSequence.Add(item);
        }

"list" is a collection of IEnumerable, i.e.

IEnumerable<Event> 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"?

like image 876
Cynthia Avatar asked Mar 12 '10 22:03

Cynthia


1 Answers

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.

like image 103
AxelEckenberger Avatar answered Sep 20 '22 04:09

AxelEckenberger