Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Array::IndexOf Method (Array^, Object^)

 

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public:
static int IndexOf(
	Array^ array,
	Object^ value
)

Parameters

array
Type: System::Array^

The one-dimensional array to search.

value
Type: System::Object^

The object to locate in array.

Return Value

Type: System::Int32

The index of the first occurrence of value in array, if found; otherwise, the lower bound of the array minus 1.

Exception Condition
ArgumentNullException

array is null.

RankException

array is multidimensional.

This method searches all the elements of a one-dimensional arrayfor value. To determine whether value exists in array, the method performs an equality comparison by calling each element's Equals method until it finds a match. This means that if the element overrides the Object::Equals(Object^) method, that override is called.

Because most arrays have a lower bound of zero, this method generally returns –1 ifvalue isn’t found. In the rare case that the lower bound of the array is equal to Int32::MinValue(0x80000000) and value isn’t found, this method returns Int32::MaxValue (0x7FFFFFFF).

This method is an O(n) operation, where n is the Length of array.

The example calls the following three overloads of the IndexOfmethod to find the index of a string in a string array:

  • IndexOf(Array^, Object^), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array^, Object^, Int32), to determine the first occurrence of the string "the" in the fourth to the last elements of a string array.

  • IndexOf(Array^, Object^, Int32, Int32), to determine the first occurrence of the string "the" in a string array from the element that follows the last successful match to the end of the array.

using namespace System;

void main()
{
   // Create a string array with 3 elements having the same value.
   array<String^>^ strings = { "the", "quick", "brown", "fox",
                               "jumps", "over", "the", "lazy", "dog",
                               "in", "the", "barn" };

   // Display the elements of the array.
   Console::WriteLine("The array contains the following values:");
   for (int i = strings->GetLowerBound(0); i <= strings->GetUpperBound(0); i++)
      Console::WriteLine("   [{0,2}]: {1}", i, strings[i]);

   // Search for the first occurrence of the duplicated value.
   String^ searchString =  "the";
   int index = Array::IndexOf(strings, searchString);
   Console::WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                      searchString, index);

   // Search for the first occurrence of the duplicated value in the last section of the array.
   index = Array::IndexOf( strings, searchString, 4);
   Console::WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                      searchString, index);

   // Search for the first occurrence of the duplicated value in a section of the array.
   int position = index + 1;
   index = Array::IndexOf(strings, searchString, position, strings->GetUpperBound(0) - position + 1);
   Console::WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                      searchString, position, strings->GetUpperBound(0), index);
}
// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: