
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Check whether an element is contained in the ArrayList in C#
To check whether an element is contained in the ArrayList, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(){ ArrayList list = new ArrayList(); list.Add("One"); list.Add("Two"); list.Add("Three"); list.Add("Four"); list.Add("Five"); list.Add("Six"); list.Add("Seven"); list.Add("Eight"); Console.WriteLine("ArrayList elements..."); foreach(string str in list){ Console.WriteLine(str); } Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly); Console.WriteLine("Does the element Six in the ArrayList? = "+list.Contains("Six")); } }
Output
This will produce the following output −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False Does the element Six in the ArrayList? = True
Example
Let us now see another example −
using System; using System.Collections; public class Demo { public static void Main(){ ArrayList arrList = new ArrayList(); arrList.Add(100); arrList.Add(200); arrList.Add(300); arrList.Add(400); arrList.Add(500); Console.WriteLine("Display elements in a range..."); IEnumerator demoEnum = arrList.GetEnumerator(1, 3); while (demoEnum.MoveNext()) { Object ob = demoEnum.Current; Console.WriteLine(ob); } Console.WriteLine("Does the element 1000 in the ArrayList? = "+arrList.Contains(1000)); } }
Output
This will produce the following output −
Display elements in a range... 200 300 400 Does the element 1000 in the ArrayList? = False
- Related Articles
- Check existence of an element in Java ArrayList
- Check whether the content of an element is editable or not in HTML
- How to check whether element is in the array in Java?
- Replace an element in an ArrayList using the ListIterator in Java
- Get the number of elements actually contained in the ArrayList in C#
- Search an element of ArrayList in Java
- Retrieve an element from ArrayList in Java
- Get the location of an element in Java ArrayList
- Remove an element from an ArrayList using the ListIterator in Java
- How to replace an element of an ArrayList in Java?
- Check if an element is in the Collection in C#
- Check if an element is in the Queue in C#
- How to remove an element from ArrayList in Java?
- Get the index of a particular element in an ArrayList in Java
- Check if the ArrayList is read-only in C#

Advertisements