C# SortedList IndexOfValue

Description

SortedList IndexOfValue returns the zero-based index of the first occurrence of the specified value in a SortedList object.

Syntax

SortedList.IndexOfValue has the following syntax.


public virtual int IndexOfValue(
  Object value
)

Parameters

SortedList.IndexOfValue has the following parameters.

  • value - The value to locate in the SortedList object. The value can be null.

Returns

SortedList.IndexOfValue method returns The zero-based index of the first occurrence of the value parameter, if value is found in the SortedList object; otherwise, -1.

Example

The following code shows how to get the index of the element with a value using the IndexOfValue() method.


using System;// w ww  . j av a2  s.c  om
using System.Collections;

class MainClass
{

  public static void Main()
  {
    SortedList mySortedList = new SortedList();

    mySortedList.Add("NY", "New York");
    mySortedList.Add("FL", "Florida");
    mySortedList.Add("AL", "Alabama");
    mySortedList.Add("WY", "Wyoming");
    mySortedList.Add("CA", "California");

    foreach (string myKey in mySortedList.Keys)
    {
      Console.WriteLine("myKey = " + myKey);
    }

    foreach(string myValue in mySortedList.Values)
    {
      Console.WriteLine("myValue = " + myValue);
    }
    int myIndex = mySortedList.IndexOfValue("New York");
    Console.WriteLine("The index of New York is " + myIndex);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack