C# List IndexOf(T, Int32)

Description

List IndexOf(T, Int32) searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the List that extends from the specified index to the last element.

Syntax

List.IndexOf(T, Int32) has the following syntax.


public int IndexOf(
  T item,
  int index
)

Parameters

List.IndexOf(T, Int32) has the following parameters.

  • item - The object to locate in the List . The value can be null for reference types.
  • index - The zero-based starting index of the search. 0 (zero) is valid in an empty list.

Returns

List.IndexOf(T, Int32) method returns The zero-based index of the first occurrence of item within the range of elements in the List that extends from index to the last element, if found; otherwise, -1.

Example


using System;// ww w.j  a v  a2 s.  c o m
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> myData = new List<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");
        myData.Add("A");

        foreach(string myD in myData)
        {
            Console.WriteLine(myD);
        }

        Console.WriteLine(myData.IndexOf("A"));

        Console.WriteLine(myData.IndexOf("A", 3));

        Console.WriteLine(myData.IndexOf("A", 2, 2));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack