BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The SortedList Collection

The thirty-ninth part of the C# Fundamentals tutorial describes the use of the SortedList class. This dictionary collection provides a hybrid of the Hashtable and Array types; each entry being a key / value pair sorted according to the key's contents.

Keys

The Keys property is similar to the Values property. It returns an ICollection object containing all of the keys of the items in a SortedList.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

foreach(string value in veg.Keys)
{
    Console.WriteLine(value);
}

/* OUTPUT

cabbage
carrot
lettuce
potato

*/

GetValueList and GetKeyList Methods

The Values property returns a collection of all of the values within the SortedList. The functionality of this collection is limited to that of the ICollection interface. The GetValueList method returns the same information as Values in a read only collection that implements the IList interface. The GetKeyList method gives the same functionality for the keys in the SortedList. These methods are useful because IList has richer behaviour than ICollection.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

IList keys = veg.GetKeyList();
IList values = veg.GetValueList();

ContainsKey

All collections that implement IDictionary include a method named Contains that returns a Boolean value indicating if a specified key exists within the collection. The SortedList includes this method and another named ContainsKey that provides exactly the same functionality.

ContainsValue

In addition to determining if a specific key exists within a SortedList, it can be useful to check if a value occurs within the collection. ContainsValue operates in a similar manner to Contains and ContainsKey; the object to test for is passed as the only parameter.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

Console.WriteLine(veg.ContainsValue("Savoy"));          // Outputs "True"
Console.WriteLine(veg.ContainsValue("Cos"));            // Outputs "False"

IndexOfKey

The IndexOfKey method improves upon the ContainsKey method. Rather than returning true or false to indicate if the specified key exists, an integer value is returned. If the value is zero or greater, this is the index of the matching key entry. If the key is not found, the method returns -1.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

Console.WriteLine(veg.IndexOfKey("potato"));            // Outputs "3"
Console.WriteLine(veg.IndexOfKey("parsnip"));           // Outputs "-1"

IndexOfValue

In addition to being able to locate a key, it is possible to find the index of a value within a SortedList. The IndexOfValue method returns the index number of a value matching the passed criteria. If the value is not found, the result is -1. If there are multiple occurrences of the value within the collection then the index of the first occurrence is returned.

SortedList veg = new SortedList();

veg["cabbage"] = "Savoy";
veg["potato"] = "King Edward";
veg["lettuce"] = "Iceberg";
veg["carrot"] = "Imperator";

Console.WriteLine(veg.IndexOfValue("King Edward"));     // Outputs "3"
Console.WriteLine(veg.IndexOfValue("Cos"));             // Outputs "-1"
29 May 2007