13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

Anonymous types227That certa<strong>in</strong>ly works, and for just a s<strong>in</strong>gle property the syntax for sett<strong>in</strong>g the name(the part <strong>in</strong> bold) is not too clumsy—but if you were copy<strong>in</strong>g several properties itwould get tiresome. <strong>C#</strong> 3 provides a shortcut: if you don’t specify the property name,but just the expression to evaluate for the value, it will use the last part of the expressionas the name—provided it’s a simple field or property. This is called a projection <strong>in</strong>itializer.It means we can rewrite the previous code asnew { person.Name, IsAdult = (person.Age >= 18) }It’s quite common for all the bits of an anonymous object <strong>in</strong>itializer to be projection<strong>in</strong>itializers—it typically happens when you’re tak<strong>in</strong>g some properties from one objectand some properties from another, often as part of a jo<strong>in</strong> operation. Anyway, I’m gett<strong>in</strong>gahead of myself. List<strong>in</strong>g 8.6 shows the previous code <strong>in</strong> action, us<strong>in</strong>g theList.ConvertAll method and an anonymous delegate.List<strong>in</strong>g 8.6Transformation from Person to a name and adulthood flagList family = new List{new Person {Name="Holly", Age=31},new Person {Name="Jon", Age=31},new Person {Name="Tom", Age=4},new Person {Name="Rob<strong>in</strong>", Age=1},new Person {Name="William", Age=1}};var converted = family.ConvertAll(delegate(Person person){ return new { person.Name, IsAdult = (person.Age >= 18) }; });foreach (var person <strong>in</strong> converted){Console.WriteL<strong>in</strong>e("{0} is an adult? {1}",person.Name, person.IsAdult);}In addition to the use of a projection <strong>in</strong>itializer for the Name property, list<strong>in</strong>g 8.6 showsthe value of delegate type <strong>in</strong>ference and anonymous methods. Without them, wecouldn’t have reta<strong>in</strong>ed our strong typ<strong>in</strong>g of converted, as we wouldn’t have been ableto specify what the TOutput type parameter of Converter should be. As it is, we caniterate through the new list and access the Name and IsAdult properties as if we wereus<strong>in</strong>g any other type.Don’t spend too long th<strong>in</strong>k<strong>in</strong>g about projection <strong>in</strong>itializers at this po<strong>in</strong>t—theimportant th<strong>in</strong>g is to be aware that they exist, so you won’t get confused when you seethem later. In fact, that advice applies to this entire section on anonymous types—sowithout go<strong>in</strong>g <strong>in</strong>to details, let’s look at why they’re present at all.8.5.4 What’s the po<strong>in</strong>t?I hope you’re not feel<strong>in</strong>g cheated at this po<strong>in</strong>t, but I sympathize if you do. Anonymoustypes are a fairly complex solution to a problem we haven’t really encountered yet…except that I bet you have seen part of the problem before, really.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!