Check whether an element is contained in the ArrayList in CSharp

Description

The following code shows how to check whether an element is contained in the ArrayList.

Example


using System;/*from w w w  . ja  v a 2s.  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);

    foreach(string s in myArrayList){
       Console.WriteLine(s);
    }


    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.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary