15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

254 ❘ ChaPTer 10 cOllectiOns<br />

following example, a list of Racer objects is fi lled. Because List < T > implements IEnumerable < T > ,<br />

the ToLookup() method can be invoked on the racers list. This method requires a delegate of type<br />

Func < TSource, TKey > that defi nes the selector of the key. Here the racers are selected based on the<br />

country by using the Lambda expression r = > r.Country . The foreach loop accesses only the racers<br />

from Australia by using the indexer.<br />

You can read more about extension methods in Chapter 11, “ Language Integrated<br />

Query. ” Lambda expressions are explained in Chapter 8.<br />

var racers = new List < Racer > ();<br />

racers.Add(new Racer("Jacques", "Villeneuve", "Canada", 11));<br />

racers.Add(new Racer("Alan", "Jones", "Australia", 12));<br />

racers.Add(new Racer("Jackie", "Stewart", "United Kingdom", 27));<br />

racers.Add(new Racer("James", "Hunt", "United Kingdom", 10));<br />

racers.Add(new Racer("Jack", "Brabham", "Australia", 14));<br />

var lookupRacers = racers.ToLookup(r = > r.Country);<br />

foreach (Racer r in lookupRacers["Australia"])<br />

{<br />

Console.WriteLine(r);<br />

}<br />

The output shows the racers from Australia:<br />

Alan Jones<br />

Jack Brabham<br />

sorted dictionary<br />

code snippet LookupSample/Program.cs<br />

SortedDictionary < TKey, TValue > is a binary search tree where the items are sorted based on the key.<br />

The key type must implement the interface IComparable < TKey > . If the key type is not sortable, you can<br />

also create a comparer implementing IComparer < TKey > <strong>and</strong> assign the comparer as a constructor argument<br />

of the sorted dictionary.<br />

Earlier in this chapter you read about SortedList < TKey, TValue > . SortedDictionary < TKey, TValue ><br />

<strong>and</strong> SortedList < TKey, TValue > have similar functionalities. But because SortedList < TKey, TValue > is<br />

implemented as a list that is based on an array <strong>and</strong> SortedDictionary < TKey, TValue > is implemented as<br />

a dictionary, the classes have different characteristics:<br />

➤ SortedList < TKey, TValue > uses less memory than SortedDictionary < TKey, TValue > .<br />

➤<br />

➤<br />

SortedDictionary < TKey, TValue > has faster insertion <strong>and</strong> removal of elements.<br />

When populating the collection with already sorted data, SortedList < TKey, TValue > is faster, if<br />

capacity changes are not needed.<br />

SortedList consumes less memory than SortedDictionary . SortedDictionary is<br />

faster with inserts <strong>and</strong> with the removal of unsorted data.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!