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.

linQ overview ❘ 269<br />

}<br />

}<br />

}<br />

public string ToString(string format)<br />

{<br />

return ToString(format, null);<br />

}<br />

public string ToString(string format, IFormatProvider formatProvider)<br />

{<br />

switch (format)<br />

{<br />

case null:<br />

case "N":<br />

return ToString();<br />

case "F":<br />

return FirstName;<br />

case "L":<br />

return LastName;<br />

case "C":<br />

return Country;<br />

case "S":<br />

return Starts.ToString();<br />

case "W":<br />

return Wins.ToString();<br />

case "A":<br />

return String.Format("{0} {1}, {2}; starts: {3}, wins: {4}",<br />

FirstName, LastName, Country, Starts, Wins);<br />

default:<br />

throw new FormatException(String.Format(<br />

"Format {0} not supported", format));<br />

}<br />

}<br />

code snippet DataLib/Racer.cs<br />

A second entity class is Team. This class just contains the name <strong>and</strong> an array of years for constructor<br />

championships.<br />

[Serializable]<br />

public class Team<br />

{<br />

public Team(string name, params int[] years)<br />

{<br />

this.Name = name;<br />

this.Years = years;<br />

}<br />

public string Name { get; private set; }<br />

public int[] Years { get; private set; }<br />

}<br />

code snippet DataLib/Team.cs<br />

The class Formula1 returns a list of racers in the method GetChampions(). The list is filled with all<br />

Formula-1 champions from the years 1950 to 2008:<br />

using System.Collections.Generic;<br />

namespace Wrox.ProCSharp.LINQ<br />

{<br />

public static class Formula1<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!