Export (0) Print
Expand All

SortedList.Contains Method

Determines whether a SortedList object contains a specific key.

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

public virtual bool Contains (
	Object key
)
public boolean Contains (
	Object key
)
public function Contains (
	key : Object
) : boolean
Not applicable.

Parameters

key

The key to locate in the SortedList object.

Return Value

true if the SortedList object contains an element with the specified key; otherwise, false.

Exception typeCondition

ArgumentNullException

key is a null reference (Nothing in Visual Basic).

InvalidOperationException

The comparer throws an exception.

The elements of a SortedList object are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves.

Contains implements IDictionary.Contains. It behaves exactly as ContainsKey.

This method uses a binary search algorithm; therefore, this method is an O(log 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 whether a SortedList object contains a specific element.

using System;
using System.Collections;

public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 2, "two" );
      mySL.Add( 4, "four" );
      mySL.Add( 1, "one" );
      mySL.Add( 3, "three" );
      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 {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" );
      myKey = 6;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, mySL.ContainsKey( myKey ) ? "in the SortedList" : "NOT in the SortedList" );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" );
      myValue = "nine";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, mySL.ContainsValue( myValue ) ? "in the SortedList" : "NOT in the SortedList" );
   }


   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 in the SortedList.
The key "6" is NOT in the SortedList.
The value "three" is in the SortedList.
The value "nine" is NOT in the SortedList.
*/ 

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)2, "two");
        mySL.Add((Int32)4, "four");
        mySL.Add((Int32)1, "one");
        mySL.Add((Int32)3, "three");
        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 {1}.", 
            System.Convert.ToString(myKey), 
            (mySL.ContainsKey((Int32)myKey)) ? 
            "in the SortedList" : "NOT in the SortedList");
        myKey = 6;
        Console.WriteLine("The key \"{0}\" is {1}.", 
            System.Convert.ToString(myKey), 
            (mySL.ContainsKey((Int32)myKey)) ? 
            "in the SortedList" : "NOT in the SortedList");

        // Searches for a specific value.
        String myValue = "three";
        Console.WriteLine("The value \"{0}\" is {1}.", myValue, 
            (mySL.ContainsValue(myValue)) ? 
            "in the SortedList" : "NOT in the SortedList");
        myValue = "nine";
        Console.WriteLine("The value \"{0}\" is {1}.", myValue, 
            (mySL.ContainsValue(myValue)) ? 
            "in the SortedList" : "NOT in the SortedList");
    } //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 in the SortedList.
 The key "6" is NOT in the SortedList.
 The value "three" is in the SortedList.
 The value "nine" is NOT in the SortedList.
 */

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