Search object and returns the zero-based index of the first occurrence within List in CSharp

Description

The following code shows how to search object and returns the zero-based index of the first occurrence within List.

Example


using System;//from  w  ww  .  j a  v  a 2 s  . co 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"));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary