Search for object and returns the index of the first occurrence within the entire one-dimensional Array in CSharp

Description

The following code shows how to search for object and returns the index of the first occurrence within the entire one-dimensional Array.

Example


using System;/*from w w  w. j  a v a  2s  .c  o  m*/
public class SamplesArray  {

   public static void Main()  {
      Array myArray=Array.CreateInstance( typeof(String), 12 );
      myArray.SetValue( "the", 0 );
      myArray.SetValue( "quick", 1 );
      myArray.SetValue( "jumps", 4 );
      myArray.SetValue( "over", 5 );
      myArray.SetValue( "the", 6 );
      myArray.SetValue( "dog", 8 );
      myArray.SetValue( "in", 9 );
      myArray.SetValue( "the", 10 );

      String myString = "the";
      int myIndex = Array.IndexOf( myArray, myString );
      Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var