C# Collection IndexOf

Description

searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection.

Syntax

Collection.IndexOf has the following syntax.


public int IndexOf(
  T item
)

Parameters

Collection.IndexOf has the following parameters.

  • item - The object to locate in the List. The value can be null for reference types.

Returns

Collection.IndexOf method returns The zero-based index of the first occurrence of item within the entire Collection, if found; otherwise, -1.

Example

The following code example demonstrates many of the properties and methods of Collection.


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

public class Demo
{
    public static void Main()
    {
        Collection<string> myData = new Collection<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");
 
        Console.WriteLine(myData.IndexOf("D"));

    }
}

The code above generates the following result.





















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




Collection