How to check if any element in a list satisfies a condition with C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program {
   static void Main(string[] args) {
        List<int> list = new List<int>() { 3, 2, 7, 1, 8 };
   
        bool exists = list.Exists(e => e > 4);
   
        if (exists) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
   }
}



/*
run:

Yes

*/

 



answered Mar 30, 2024 by avibootz
...