想象一下struct
:
struct Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
以下代码:
var list = new List<Person>();
list.Add(new Person { FirstName = "F1", LastName = "L1" });
list.Add(new Person { FirstName = "F2", LastName = "L2" });
list.Add(new Person { FirstName = "F3", LastName = "L3" });
// Can't modify the expression because it's not a variable
list[1].FirstName = "F22";
当我想更改Property
' 的值时,它会给我以下错误:
Can't modify the expression because it's not a variable
同时,当我尝试在数组中更改它时,例如Person[]
它可以正常工作而没有任何错误。与泛型集合一起使用时,我的代码有任何问题吗?