Export (0) Print
Expand All

SortedList.IndexOfValue Method

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

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual int IndexOfValue (
	Object value
)
public int IndexOfValue (
	Object value
)
public function IndexOfValue (
	value : Object
) : int
Not applicable.

Parameters

value

The value to locate in the SortedList object. The value can be a null reference (Nothing in Visual Basic).

Return Value

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

The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element is removed, the indexing also adjusts accordingly. Therefore, the index of a specific key/value pair might change as elements are added or removed from the SortedList object.

The values of the elements of the SortedList are compared to the specified value using the Equals method.

This method uses a linear search; therefore, this method is an O(n) operation, where n is Count.

Starting with the .NET Framework 2.0, this method uses the collection’s objects’ Equals and CompareTo methods on item to determine whether item exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the item parameter on the objects in the collection.

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

using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 1, "one" );
      mySL.Add( 3, "three" );
      mySL.Add( 2, "two" );
      mySL.Add( 4, "four" );
      mySL.Add( 0, "zero" );

      // Displays the values of the SortedList.
      Console.WriteLine( "The SortedList contains the following values:" );
      PrintIndexAndKeysAndValues( mySL );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) );
   }


   public static void PrintIndexAndKeysAndValues( SortedList myList )  {
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i, myList.GetKey(i), myList.GetByIndex(i) );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The SortedList contains the following values:
    -INDEX-    -KEY-    -VALUE-
    [0]:    0    zero
    [1]:    1    one
    [2]:    2    two
    [3]:    3    three
    [4]:    4    four

The key "2" is at index 2.
The value "three" is at index 3.
*/ 

import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[] args)
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();

        mySL.Add((Int32)1, "one");
        mySL.Add((Int32)3, "three");
        mySL.Add((Int32)2, "two");
        mySL.Add((Int32)4, "four");
        mySL.Add((Int32)0, "zero");

        // Displays the values of the SortedList.
        Console.WriteLine("The SortedList contains the following values:");
        PrintIndexAndKeysAndValues(mySL);

        // Searches for a specific key.
        int myKey = 2;

        Console.WriteLine("The key \"{0}\" is at index {1}.", 
            System.Convert.ToString(myKey), 
            System.Convert.ToString(mySL.IndexOfKey((Int32)myKey)));

        // Searches for a specific value.
        String myValue = "three";

        Console.WriteLine("The value \"{0}\" is at index {1}.", 
            System.Convert.ToString(myValue), 
            System.Convert.ToString(mySL.IndexOfValue(myValue)));
    } //main

    public static void PrintIndexAndKeysAndValues(SortedList myList)
    {
        Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.get_Count(); i++) {
            Console.WriteLine("\t[{0}]:\t{1}\t{2}", 
                System.Convert.ToString(i), 
                System.Convert.ToString(myList.GetKey(i)), 
                System.Convert.ToString(myList.GetByIndex(i)));
        }
        Console.WriteLine();
    } //PrintIndexAndKeysAndValues
} //SamplesSortedList


/* 
 This code produces the following output.
 
 The SortedList contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    0       zero
        [1]:    1       one
        [2]:    2       two
        [3]:    3       three
        [4]:    4       four

 The key "2" is at index 2.
 The value "three" is at index 3.
 */

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft