.Net framework ArrayList Contains() method allow us to determine whether an element is in the ArrayList. this arraylist Contains() method exists under System.Collections namespace. this Contains() method has a required parameter named ‘item’.
the ‘item’ property value type is System.Object, which represents the Object to locate in the ArrayList. the value can be null. Contains() method return value data type is System.Boolean. this method return ‘true’, if the ‘item’ is found in the arraylist; otherwise method return ‘false’.
the Contains() method implements as IList.Contains(Object) and this method perform a linear search. Contains() method determines equality by calling Object.Equals.
the following console c# example code demonstrate us how can we determine whether an element exists in arraylist programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Bisque", "Aqua", "DarkOrange", "Salmon" }; Console.WriteLine("ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } Console.WriteLine("\n'Salmon' element exists in ArrayList? " + colors.Contains("Salmon")); Console.WriteLine("\n'Green' element exists in ArrayList? " + colors.Contains("Green")); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
ArrayList Elements.... Bisque Aqua DarkOrange Salmon 'Salmon' element exists in ArrayList? True 'Green' element exists in ArrayList? False |