Search array element using Array.IndexOf method - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Search array element using Array.IndexOf method

Demo Code

using System;// w w w .  j a  va 2  s . c  om
class DotNETSearcher
{
   public static void Main()
   {
      int searchKey;
      int searchResult;
      int [] testScores = {32, 2, 87, 23, 2, 51, 10, 1, 22, 45, 33, 10};
      do
      {
         Console.Write("Please enter the test score you would like to locate: ");
         searchKey = Convert.ToInt32(Console.ReadLine());
         searchResult = Array.IndexOf(testScores, searchKey);
         if (searchResult >= 0)
            Console.WriteLine("The score was found in position: {0}\n", searchResult);
         else
            Console.WriteLine("The score was not found\n");
         Console.Write("Would you like to do another search? Y)es N)o ");
      } while (Console.ReadLine().ToUpper() == "Y");
   }
}

Result


Related Tutorials