IndexOf(Object, Int32) method in ArrayList

In this article I will discuss the IndexOf(Object, Int32) method of ArrayList.
  • 4054

Introduction

IndexOf(Object, Int32) method searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element. That means that this method searches from the index value that we give and return their index value.

Example

namespace ConsoleApplication3

{

    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",0));

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

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

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

        }

    }

}


Output

indexof.jpg
 

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.