C# ArrayList Contains
Description
ArrayList Contains
determines whether an element is
in the ArrayList.
Syntax
ArrayList.Contains
has the following syntax.
public virtual bool Contains(
Object item
)
Parameters
ArrayList.Contains
has the following parameters.
item
- The Object to locate in the ArrayList. The value can be null.
Returns
ArrayList.Contains
method returns true if item is found in the ArrayList; otherwise, false.
Example
The following code
uses the Contains()
method to
determine if the string 'text' is in the ArrayList
.
using System;//from ww w . jav a2 s . c o m
using System.Collections;
class MainClass
{
public static void Main()
{
ArrayList myArrayList = new ArrayList();
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
myArrayList.Add("A");
string[] anotherStringArray = {"Here's", "some", "more", "text"};
myArrayList.SetRange(0, anotherStringArray);
if (myArrayList.Contains("text"))
{
int index = myArrayList.IndexOf("text");
Console.WriteLine("myArrayList does contain the word 'text'");
Console.WriteLine("'text' first occurs at index " + index);
index = myArrayList.LastIndexOf("text");
Console.WriteLine("'text' last occurs at index " + index);
}
}
}
The code above generates the following result.