Searching an array in C# – A simple example

By | April 17, 2011

Hi,

Want to see a simple example on how to search an array in c# ? See the following lines of code.

using System;

class MainClass {
  public static void Main() {
    int[] myArray = new int[25];
    int test;
    bool search= false;

    for(int i = 0; i < 25; i++)
      myArray [i] = i;

    test= 17;

    foreach(int m in myArray ) {
      if(m == test) {
        search= true;
        break;
      }
    }

    if(search)
      Console.WriteLine("Yahooo! Found it in Array!!!");
  }
}

Output-“Yahooo! Found it in Array!!!”

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *