IndexOf method in ArrayList

In this article I will explain the IndexOf method of arraylist.
  • 3553

IndexOf(Object) method searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.

Example

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            ArrayList arr = new ArrayList();

            arr.Add("Red");

            arr.Add("Pink");

            arr.Add("Orange");

            arr.Add("Yellow");

            arr.Add("Blue");

            arr.Add("Gray");

            arr.Add("White");

            Console.WriteLine("The elements in the arraylist are:");

            foreach (object obj in arr)

            {

                Console.WriteLine(obj);

            }

            Console.WriteLine("The index value of Red is:" + arr.IndexOf("Red"));

            Console.WriteLine("The index value of Orange is:" + arr.IndexOf("Orange"));

            Console.WriteLine("The index value of White is:" + arr.IndexOf("White"));

            Console.WriteLine("The index value of Blue is:" + arr.IndexOf("Blue"));

        }

    }

}


Output

 

indexof.jpg 

 

Further Readings
 
You may also want to read these related articles.

Ask Your Question  

Got a programming related question? You may want to post your question here

 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.