15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

246 ❘ ChaPTer 10 cOllectiOns<br />

}<br />

}<br />

documentList.RemoveFirst();<br />

return doc;<br />

In the Main() method, the PriorityDocumentManager is used to demonstrate its functionality.<br />

Eight new documents with different priorities are added to the linked list, <strong>and</strong> then the complete list<br />

is displayed:<br />

static void Main()<br />

{<br />

PriorityDocumentManager pdm = new PriorityDocumentManager();<br />

pdm.AddDocument(new Document("one", "Sample", 8));<br />

pdm.AddDocument(new Document("two", "Sample", 3));<br />

pdm.AddDocument(new Document("three", "Sample", 4));<br />

pdm.AddDocument(new Document("four", "Sample", 8));<br />

pdm.AddDocument(new Document("five", "Sample", 1));<br />

pdm.AddDocument(new Document("six", "Sample", 9));<br />

pdm.AddDocument(new Document("seven", "Sample", 1));<br />

pdm.AddDocument(new Document("eight", "Sample", 1));<br />

}<br />

pdm.DisplayAllNodes();<br />

code snippet LinkedListSample/Program.cs<br />

With the processed result, you can see that the documents are sorted fi rst by the priority <strong>and</strong> second by<br />

when the document was added:<br />

priority: 9, title six<br />

priority: 8, title one<br />

priority: 8, title four<br />

priority: 4, title three<br />

priority: 3, title two<br />

priority: 1, title five<br />

priority: 1, title seven<br />

priority: 1, title eight<br />

sorTed lisT<br />

If the collection you need should be sorted based on a key, you can use SortedList < TKey, TValue > .<br />

This class sorts the elements based on a key. Not only can you use any type for the value, but also for<br />

the key.<br />

The example creates a sorted list where both the key <strong>and</strong> the value are of type string . The default constructor<br />

creates an empty list, <strong>and</strong> then two books are added with the Add() method. With overloaded constructors,<br />

you can defi ne the capacity of the list <strong>and</strong> also pass an object that implements the interface IComparer < TKey > ,<br />

which is used to sort the elements in the list.<br />

The fi rst parameter of the Add() method is the key (the book title); the second parameter is the value (the<br />

ISBN number). Instead of using the Add() method, you can use the indexer to add elements to the list.<br />

The indexer requires the key as index parameter. If a key already exists, the Add() method throws an<br />

exception of type ArgumentException . If the same key is used with the indexer, the new value replaces<br />

the old value.<br />

SortedList < TKey, TValue > allows only one value per key. If you need multiple values<br />

per key you can use Lookup < TKey , TElement > .<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!