1

I have List<QueueItem> QueueItemList list of objects. I am filtering the objects list by object property Status and assign filtered list to processingList. If I will change object Status property in the QueueItemList list does it will be changed in processingList too?

        public List<QueueItem> GetItems()
        {
            lock (Locker)
            {
                return QueueItemList.ToList();
            }
        }

var processingList = GetItems.Where(p => p.Status== QueueItemStatus.Processing).ToList();

If I will change object Status property in the QueueItemList list does it will be changed in processingList too?

Yes, it will, unless QueueItem is a value type (struct). If it is a class then it is a reference type, meaning that both processingList and QueueItemList are pointing to the exact same memory location for their elements. Those variables are just pointers.

4

4 回答 4

2

Yes because you are sharing a reference to the same underlying object, assuming of course the item in the list is a class (or other reference type) and not a struct (value type).

Do note that you have two independent lists of references, but the references in these lists point to the same set of objects.

If you re-assign the entire instance in one list (as in list[0] = new MyClass()), the re-assignment will not occur in the other list, but I cannot envisage a use case for this anyway so it shouldn't be a concern.

于 2012-07-05T08:02:30.503 回答
2

如果我将更改 QueueItemList 列表中的对象状态属性,它是否也会在 processingList 中更改?

是的,它会,除非QueueItem是值类型 ( struct)。如果它是 aclass那么它是一个引用类型,这意味着它们processingListQueueItemList都指向它们元素的完全相同的内存位置。这些变量只是指针。

于 2012-07-05T08:02:28.600 回答
0

Yes it will changed in processingList too.

于 2012-07-05T08:02:59.303 回答
0

Yes, you will have N references to the same object. In other words: you will be changing the same object.

于 2012-07-05T08:03:32.510 回答