C# SortedList Contains
Description
SortedList Contains
determines whether a SortedList
object contains a specific key.
Syntax
SortedList.Contains
has the following syntax.
public virtual bool Contains(
Object key
)
Parameters
SortedList.Contains
has the following parameters.
key
- The key to locate in the SortedList object.
Returns
SortedList.Contains
method returns true if the SortedList object contains an element with the specified key;
otherwise, false.
Example
SortedList.Contains
determines whether a SortedList
object contains a specific key.
using System;//from w w w . j av a 2 s . c om
using System.Collections;
public class SamplesSortedList {
public static void Main() {
SortedList mySL = new SortedList();
mySL.Add( 2, "two" );
mySL.Add( 4, "four" );
mySL.Add( 1, "one" );
mySL.Add( 3, "three" );
mySL.Add( 0, "zero" );
PrintIndexAndKeysAndValues( mySL );
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" );
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();
}
}
The code above generates the following result.