简体   繁体   English

添加到List后修改项目不更新列表

[英]Modifying item after addition to List does not update list

I have a member variable which is a list of reference types. 我有一个成员变量,它是一个引用类型列表。 In a method I create and item, add it to the list. 在我创建和项目的方法中,将其添加到列表中。 The item is then updated point to another instance but the instance in the list does not get updated (his is in some unit test code). 然后该项目更新指向另一个实例,但列表中的实例不会更新(他在某些单元测试代码中)。 Example looks like 示例看起来像

Item localItem = new Item(arg1, arg2);
this.itemList.Add(localItem);

// Do some testing and assertions

localItem = new Item(arg3, arg4);  // This does not update the instance of
                                   // localItem in this.ItemList

// Do some more testing and assertions

I can update my tests to fix this but it still caught me by surprise. 我可以更新我的测试来解决这个问题,但它仍然让我感到意外。 I supose the List wants to keep the original instance passed through the Add method and cares not if the local variable which was used to pass it in now points to something else. 我想让List希望保持原始实例通过Add方法传递,并且如果现在用于传递它的局部变量指向其他东西则不在乎。 Can anyone maybe confirm this understanding or explain it more clearly? 任何人都可以证实这种理解或更清楚地解释它吗?

Ofcourse not. 当然不是。 The localItem is a reference to the actual object (not the object itself). localItem是对实际对象(不是对象本身)的引用。 The list contains also a reference to this same object. 该列表还包含对此同一对象的引用。 If you assign another item to localItem it now points to another object but the list has still a reference to the old object. 如果您将另一个项目分配给localItem它现在指向另一个对象,但该列表仍然具有对旧对象的引用。

If you want the object in the list to update automatically you must introduce a new object that holds a reference to the item and add this one to the list 如果希望列表中的对象自动更新,则必须引入一个新对象,该对象包含对该项的引用并将此对象添加到列表中

public class ItemReference
{
    public Item Item { get; set; }
}

Now you can create a local item like this 现在您可以创建这样的本地项目

ItemReference itemRef = new ItemReference();
itemRef.Item = new Item(arg1, arg2);

this.itemReferenceList.Add(itemRef);
itemRef.Item = new Item(arg3, arg4);

Because the list now has a reference to the same (unchanged) itemRef it "sees" the new item as well. 因为列表现在具有对相同(未更改) itemRef的引用, itemRef它也“看到”新项目。

The list has to be declared as 该列表必须声明为

List<ItemReference> itemReferenceList;

When you add localItem to the list you add a reference to the object instance (assuming Item is a reference type aka a class ) - when you subsequently create a new Item instance the result is a new reference (similar to a pointer) - if you want to update the list you have to remove the old item and add the new item - these are two totally different instances. localItem添加到列表时,添加对对象实例的引用 (假设Item是引用类型,也就是class ) - 当您随后创建新的Item实例时,结果是新引用 (类似于指针) - 如果您想要更新列表,你必须删除旧项目并添加新项目 - 这是两个完全不同的实例。 Alternatively you can modify any properties on localItem and these will be reflected when you access the item through the list. 或者,您可以修改localItem上的任何属性,这些属性将在您通过列表访问项目时反映出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM