ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • How to get all elements of a List
    IT Story/C# & WPF 2019. 11. 20. 22:08
    반응형

    Return value: This method returns a List<T> containing all the elements that match the conditions defined by the specified predicate otherwise it returns an empty List<T>.

    Exception: This method will give ArgumentNullException if the match is null.

    Below programs illustrate the use of List<T>.FindAll(Predicate<T>) Method:

     

    Example 1:

    // C# Program to get all the element that 
    // match the specified conditions defined 
    // by the predicate 
    using System; 
    using System.Collections; 
    using System.Collections.Generic; 
      
    class Geeks { 
      
        // function which checks whether an 
        // element is even or not. Or you can 
        // say it is the specified condition 
        private static bool isEven(int i) 
        { 
            return ((i % 2) == 0); 
        } 
      
        // Main Method 
        public static void Main(String[] args) 
        { 
      
            // Creating an List<T> of Integers 
            List<int> firstlist = new List<int>(); 
      
            // Adding elements to List 
            firstlist.Add(2); 
            firstlist.Add(4); 
            firstlist.Add(7); 
            firstlist.Add(2); 
            firstlist.Add(3); 
            firstlist.Add(2); 
            firstlist.Add(4); 
      
            Console.WriteLine("Elements Present in List:\n"); 
      
            // Displaying the elements of List 
            foreach(int k in firstlist) 
            { 
                Console.WriteLine(k); 
            } 
      
            Console.WriteLine(" "); 
      
            Console.Write("Elements that Match: \n"); 
      
            // Will give the List of Elements that 
            // match the conditions defined by predicate 
            List<int> Result = new List<int>(firstlist.FindAll(isEven)); 
      
            foreach(int i in Result) 
            { 
                Console.WriteLine(i); 
            } 
        } 
    } 

     

    Example 2:

    // C# Program to get all the element that 
    // match the specified conditions defined 
    // by the predicate 
    using System; 
    using System.Collections; 
    using System.Collections.Generic; 
      
    class Geeks { 
      
        // function which checks whether an 
        // element is even or not. Or you can 
        // say it is the specified condition 
        private static bool isEven(int i) 
        { 
            return ((i % 2) == 0); 
        } 
      
        // Main Method 
        public static void Main(String[] args) 
        { 
      
            // Creating an List<T> of Integers 
            List<int> firstlist = new List<int>(); 
      
            // Adding elements to List 
            firstlist.Add(17); 
            firstlist.Add(77); 
            firstlist.Add(15); 
            firstlist.Add(9); 
            firstlist.Add(3); 
            firstlist.Add(7); 
            firstlist.Add(57); 
      
            Console.WriteLine("Elements Present in List:\n"); 
      
            // Displaying the elements of List 
            foreach(int k in firstlist) 
            { 
                Console.WriteLine(k); 
            } 
      
            Console.WriteLine(" "); 
      
            Console.Write("Result is: "); 
      
            // Will give the List of Elements that 
            // match the conditions defined by predicate 
            // Here no even number found in the list 
            // so it wil return an empty list 
            List<int> Result = new List<int>(firstlist.FindAll(isEven)); 
      
            // checking for the resultant 
            // Elements in the List 
            if ((Result.Count) == 0) { 
                Console.WriteLine("No Match Found"); 
            } 
        } 
    } 

     

    반응형

    댓글

Designed by Tistory.