C# SortedList IndexOfKey

Description

SortedList IndexOfKey returns the zero-based index of the specified key in a SortedList object.

Syntax

SortedList.IndexOfKey has the following syntax.


public virtual int IndexOfKey(
  Object key
)

Parameters

SortedList.IndexOfKey has the following parameters.

  • key - The key to locate in the SortedList object.

Returns

SortedList.IndexOfKey method returns The zero-based index of the key parameter, if key is found in the SortedList object; otherwise, -1.

Example

The following code example shows how to determine the index of a key or a value in a SortedList object.


//from   w  ww  .j  a v a 2  s  .co m
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {
      SortedList mySL = new SortedList();
      mySL.Add( 1, "one" );
      mySL.Add( 3, "three" );
      mySL.Add( 2, "two" );
      mySL.Add( 4, "four" );
      mySL.Add( 0, "zero" );

      int myKey = 2;
      Console.WriteLine(mySL.IndexOfKey( myKey ));

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack