我有一个object
它有一些属性,其中一些属性是Lists
。每个列表都包含其他类的实例。我想要做的是从列表中获取第一项并覆盖这些属性值。C# - 使用新对象值更新列表项目
这里是什么,我有一个伪例如:
public class User
{
public List<Address> Addresses = new List<Address>();
public User ()
{
Addresses = fill with data;
}
}
public class TestUser
{
public User user; // Is filled somewhere in this class
public void TestUpdateList (Address addr)
{
// The param "addr" contains new values
// These values must ALWAYS be placed in the first item
// of the "Addresses" list.
// Get the first Address object and overwrite that with
// the new "addr" object
user.Addresses[0] = addr; // <-- doesn't work, but should give you an idea
}
}
我希望这个例子阐明了我想要做一些轻。
所以我基本上在寻找一种方式来“更新”列表中的现有项目,在这种情况下是object
。
究竟不到风度的工作? TestUser不应该在里面保存一个User的实例吗? – Blachshma
它为什么不起作用?你能提供有关错误的更多细节吗? – Dutts
地址是类还是结构? – sll