Part 75 - List collection class in c# continued

This is continuation to Part 74. So, please watch Part 74, before proceeding. In this video, we will discuss1. Contains() function - Use this function to check if an item exists in the list. This method returns true if the items exists, else false.

2. Exists() function - Use this function, to check if an item exists in the list based on a condition. This method returns true if the items exists, else false.

3. Find() function - This method searches for an element that matches the conditions defined by the specified lambda expression and returns the first matching item from the list.

4. FindLast() function - This method searches for an element that matches the conditions defined by the specified lambda expression and returns the Last matching item from the list.

5. FindAll() function - This method returns all the items from the list that match the conditions specified by the lambda expression.

6. FindIndex() function - This method returns the index of the first item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.

7. FindLastIndex() function - This method returns the index of the last item, that matches the condition specified by the lambda expression. There are 2 other overloads of this method which allows us to specify the range of elements to search, with in the list.

// To check if an item exists in the list use Contains() function // This method returns true if the items exists, else falseif (listCustomers.Contains(customer2)) {Console.WriteLine("Customer2 object exists in the list"); }else {Console.WriteLine("Customer2 object does not exist in the list"); }Console.WriteLine("------------------------------------------------------");

// To check if an item exists in the list based on a condition, then use Exists() function // This method returns true if the items exists, else falseif (listCustomers.Exists(x => x.Name.StartsWith("M"))) {Console.WriteLine("List contains customer whose name starts with M"); }else {Console.WriteLine("List does not contain a customer whose name starts with M"); }Console.WriteLine("------------------------------------------------------");

// FindIndex() method returns the index of the first item, that matches the // condition specified by the lambda expression. There are 2 other overloads // of this method which allows us to specify the range of elements to // search, with in the list.Console.WriteLine("Index of the first matching customer object whose salary is greater 5000 =" + listCustomers.FindIndex(customer => customer.Salary > 5000));Console.WriteLine("------------------------------------------------------");

// FindLastIndex() method returns the index of the last item, // that matches the condition specified by the lambda expression. // There are 2 other overloads of this method which allows us to specify // the range of elements to search, with in the list.Console.WriteLine("Index of the Last matching customer object whose salary is greater 5000 = " + listCustomers.FindLastIndex(customer => customer.Salary > 5000));Console.WriteLine("------------------------------------------------------"); }}