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.

lists ❘ 235<br />

}<br />

}<br />

}<br />

else<br />

return result;<br />

case CompareType.Wins:<br />

return x.Wins.CompareTo(y.Wins);<br />

default:<br />

throw new ArgumentException("Invalid Compare Type");<br />

code snippet ListSamples/RacerComparer.cs<br />

An instance of the RacerComparer class can now be used with the Sort() method. Passing the enumeration<br />

RacerComparer.CompareType.Country sorts the collection by the property Country:<br />

racers.Sort(new RacerComparer(RacerComparer.CompareType.Country));<br />

racers.ForEach(Console.WriteLine);<br />

Another way to do the sort is by using the overloaded Sort() method, which requires a Comparison<br />

delegate:<br />

public void List.Sort(Comparison);<br />

Comparison is a delegate to a method that has two parameters of type T <strong>and</strong> a return type int. If the<br />

parameter values are equal, the method must return 0. If the first parameter is less than the second, a value<br />

less than zero must be returned; otherwise, a value greater than zero is returned:<br />

public delegate int Comparison(T x, T y);<br />

Now you can pass a Lambda expression to the Sort() method to do a sort by the number of wins. The two<br />

parameters are of type Racer, <strong>and</strong> in the implementation the Wins properties are compared by using the<br />

int method CompareTo(). In the implementation, r2 <strong>and</strong> r1 are used in the reverse order, so the number<br />

of wins is sorted in descending order. After the method has been invoked, the complete racer list is sorted<br />

based on the number of wins of the racer:<br />

racers.Sort((r1, r2) => r2.Wins.CompareTo(r1.Wins));<br />

You can also reverse the order of a complete collection by invoking the Reverse() method.<br />

Type Conversion<br />

With the List method ConvertAll(), all types of a collection can be converted to<br />

a different type. The ConvertAll() method uses a Converter delegate that is defined<br />

like this:<br />

public sealed delegate TOutput Converter(TInput from);<br />

The generic types TInput <strong>and</strong> TOutput are used with the conversion. TInput is the argument of the delegate<br />

method, <strong>and</strong> TOutput is the return type.<br />

In this example, all Racer types should be converted to Person types. Whereas the Racer type<br />

contains a firstName, lastName, country, <strong>and</strong> the number of wins, the Person type contains just a<br />

name. For the conversion, the country of the racer <strong>and</strong> race wins can be ignored, but the name must be<br />

converted:<br />

[Serializable]<br />

public class Person<br />

{<br />

private string name;<br />

public Person(string name)<br />

{<br />

this.name = name;<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!