Sorted Lists


If you need a sorted list, you can use SortedList<TKey, TValue>. This class sorts the elements based on a key.

The example creates a sorted list where both the key and the value are of type string. The default constructor creates an empty list, and then two books are added with the Add() method. With overloaded constructors, you can define the capacity of the list and also pass an object that implements the interface IComparer<TKey>, which is used to sort the elements in the list.

The first parameter of the Add() method is the key (the book title); the second parameter is the value (the ISBN number). Instead of using the Add() method, you can use the indexer to add elements to the list. The indexer requires the key as index parameter. If a key already exists, the Add() method throws an exception of type ArgumentException. If the same key is used with the indexer, the new value replaces the old value.

  SortedList<string, string> books = new SortedList<string, string>(); books.Add(".NET 2.0 Wrox Box", "978-0-470-04840-5"); books.Add("Professional C# 2005", "978-0-7645-7534-1"); books["Beginning Visual C# 2005"] = "978-0-7645-4382-1"; books["Professional C# with .NET 3.0"] = "978-0-470-12472-7"; 

You can iterate through the list by using a foreach statement. Elements that are returned by the enumerator are of type KeyValuePair<TKey, TValue>, which contains both the key and the value. The key can be accessed with the Key property, the value with the Value property.

  foreach (KeyValuePair<string, string> book in books) {    Console.WriteLine("{0}, {1}", book.Key, book.Value); } 

The iteration displays book titles and ISBN numbers ordered by the key:

 .NET 2.0 Wrox Box, 978-0-470-04840-5 Beginning Visual C# 2005, 978-0-7645-4382-1 Professional C# 2005, 978-0-7645-7534-1 Professional C# with .NET 3.0, 978-0-470-12472-7

You can also access the values and keys by using the Values and Keys properties. The Values property returns IList<TValue>; the Keys property returns IList<TKey>, so you can use these properties with a foreach:

  foreach (string isbn in books.Values) {    Console.WriteLine(isbn); } foreach (string title in books.Keys) {    Console.WriteLine(title); } 

The first loop displays the values, and next the keys:

 978-0-470-04840-5 978-0-7645-4382-1 978-0-7645-7534-1 978-0-470-12472-7 .NET 2.0 Wrox Box Beginning Visual C# 2005 Professional C# 2005 Professional C# with .NET 3.0

Properties of the SortedList<TKey, TValue> class are described in the following table.

Open table as spreadsheet

SortedList Properties

Description

Capacity

With the property Capacity you can get and set the number of elements the list can contain. The capacity behaves as List<T>: the default constructor creates an empty list; adding the first item allocates a capacity of 4 items, and then the capacity is doubled as needed.

Comparer

The property Comparer returns the comparer that is associated with the list. You can pass the comparer in the constructor. The default comparer compares the key items by invoking the method CompareTo of the IComparable<TKey> interface. Either the key type implements this interface or you have to create a custom comparer.

Count

The property Count returns the number of elements in the list.

Item

With the indexer you can access the elements in the list. The parameter type of the indexer is defined by the key type.

Keys

The property Keys returns IList<TKey> containing all keys.

Values

The property Values returns IList<TValue> containing all values.

Methods of the SortedList<T> type are similar to the other collections you’ve learned about in this chapter. The difference is that SortedList<T> requires a key and a value.

Open table as spreadsheet

SortedList Methods

Description

Add()

The Add() method adds an element with key and value to the list.

Remove() RemoveAt()

The Remove() method requires the key of the element to be removed from the list. With RemoveAt() you can remove an element at a specified index.

Clear()

The method Clear() removes all elements from the list.

ContainsKey() ContainsValue()

The ContainsKey() and ContainsValue() methods check if the list contains a specified key or value, and return true or false.

IndexOfKey() IndexOfValue()

The IndexOfKey() and IndexOfValue() methods check if the list contains a specified key or value and return the integer-based index.

TrimExcess()

The method TrimExcess() resizes the collection and changes the capacity to the required item count.

TryGetValue()

With the method TryGetValue() you can try to get the value for a specified key. If the key does not exist this method returns false. If the key exists, true is returned, and the value is returned as out parameter.

Tip 

Besides the generic SortedList<TKey, TValue> , a corresponding nongeneric list named SortedList exists.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net