开发者

Get Item From List<T> and update a property on that Item

开发者 https://www.devze.com 2023-02-17 15:09 出处:网络
I have a List of type Person that I would like to update DisplayValue based on DisplayName.How can I accomplish this?

I have a List of type Person that I would like to update DisplayValue based on DisplayName. How can I accomplish this?

public class Person
{
     public string DisplayName { get; set; }
     public string D开发者_JAVA百科isplayValue { get; set; }
     ... other properties
}


Your question is unclear, but I think this is what you're after.

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;
}


create a private variable, explicitly define the getter and setter, and modify Display value accordingly in the 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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消