Set multiple properties in a List ForEach()?

后端 未结 5 920
感情败类
感情败类 2021-01-30 05:04

Given a class:

class foo
{
    public string a = \"\";
    public int b = 0;
}

Then a generic list of them:

var list = new List         


        
5条回答
  •  暖寄归人
    2021-01-30 05:29

    list.ForEach(lamba=>lambda.a="hello!"); 
    

    Becomes

    list.ForEach(item=>{
         item.a = "hello!";
         item.b = 99;
    });
    

    Of course you can also assign them when you create the list like :

    var list = new List(new []{new foo(){a="hello!",b=99}, new foo(){a="hello2",b=88}}); 
    

提交回复
热议问题