IndexOf Método (Array, Object, Int32, Int32)
Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Array.IndexOf (Método) (Array, Object, Int32, Int32)

 

Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of ifs first occurrence. The range extends from a specified index for a specified number of elements.

Espacio de nombres:   System
Ensamblado:  mscorlib (en mscorlib.dll)

public static int IndexOf(
	Array array,
	object value,
	int startIndex,
	int count
)

Parámetros

array
Type: System.Array

The one-dimensional array to search.

value
Type: System.Object

The object to locate in array.

startIndex
Type: System.Int32

The starting index of the search. 0 (zero) is valid in an empty array.

count
Type: System.Int32

The number of elements to search.

Valor devuelto

Type: System.Int32

The index of the first occurrence of value, if it’s found,in the array from index startIndexto startIndex + count - 1; otherwise, the lower bound of the array minus 1.

Exception Condition
ArgumentNullException

array is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

-o-

count is less than zero.

-o-

startIndex and count do not specify a valid section in array.

RankException

array is multidimensional.

This method searches the elements of a one-dimensional arrayfrom startIndexto startIndex plus count minus 1, if count is greater than 0. To determine whether value exists in array, the method performs an equality comparison by calling the Equals method of every element until it finds a match. This means that if the element overrides the M:System.Object.Equals(System.Object) method, that override is called.

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

If startindex equals P:System.Array.Length, the method returns -1. If startIndex is greater than P:System.Array.Length, the method throws an T:System.ArgumentOutOfRangeException.

This method is an O(n) operation, where n is count.

The example calls the following three overloads of the M:System.Array.IndexOf(System.Array,System.Object) method 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. To determine the value of the count argument, it subtracts the upper bound of the array from the starting index and adds one.

using System;

public class Example
{
    public static void Main()
    {
       // Create a string array with 3 elements having the same value.
       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
Disponible desde 4.5
.NET Framework
Disponible desde 1.1
Portable Class Library
Compatible con: portable .NET platforms
Silverlight
Disponible desde 2.0
Windows Phone Silverlight
Disponible desde 7.0
Windows Phone
Disponible desde 8.1
Volver al principio
Mostrar:
© 2016 Microsoft