Check if all List Items have the same member value in C#

后端 未结 6 1512
遇见更好的自我
遇见更好的自我 2020-12-19 10:32

I\'m searching for a simple and fast way to check if all my Listitems have the same value for a member.

foreach (Item item in MyList)
{
    Item.MyMember = &         


        
6条回答
  •  死守一世寂寞
    2020-12-19 11:07

    Your own solution is simple enough already, but if you wanted to abstract away the loop and write it more expressively, you could use Linq.

    bool allSame = MyList.All(item => item.MyMember == someValue);
    

提交回复
热议问题