我有一个List类型Person,我想根据DisplayName更新DisplayValue。我怎样才能做到这一点?从列表中获取项目<T>并更新该项目的属性
public class Person
{
public string DisplayName { get; set; }
public string DisplayValue { get; set; }
... other properties
}
我有一个List类型Person,我想根据DisplayName更新DisplayValue。我怎样才能做到这一点?从列表中获取项目<T>并更新该项目的属性
public class Person
{
public string DisplayName { get; set; }
public string DisplayValue { get; set; }
... other properties
}
你的问题不清楚,但我认为这就是你要的。
List<Person> persons;
var person = persons.Find(p => p.DisplayName == "Fred");
person.DisplayValue = "Flinstone";
var list = ... List<Person> from somewhere.
foreach(var person in list){
person.DisplayValue = person.DisplayName;
}
如果他更新'public string DisplayValue {get;组; } to'public string DisplayValue {get {return this.DisplayName; }}' – 2011-03-16 04:55:45
他实际上可能打算做Jason建议的事情。我的回答实际上没有太大意义,但它确实做了什么问题?我认为? – Jon 2011-03-16 05:05:50
创建专用变量,显式地定义getter和setter,并在设定器相应地修改显示值。
public class Person
{
private string displayName;
public string DisplayName
{
get { return displayName; }
set
{
displayName = value;
DisplayValue = value + " is a value";
}
}
public string DisplayValue { get; set; }
//... other properties
}
>>更新DisplayValue基于DisplayName <<您能否请澄清,基于如何?并更新如何? – 2011-03-16 05:01:18