13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

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

224 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compiler8.5 Anonymous typesImplicit typ<strong>in</strong>g, object and collection <strong>in</strong>itializers, and implicit array typ<strong>in</strong>g are all useful<strong>in</strong> their own right, to a greater or lesser extent. However, they all really serve ahigher purpose—they make it possible to work with our f<strong>in</strong>al feature of the chapter,anonymous types. They, <strong>in</strong> turn, serve a higher purpose—LINQ.8.5.1 First encounters of the anonymous k<strong>in</strong>dIt’s much easier to expla<strong>in</strong> anonymous types when you’ve already got some idea ofwhat they are through an example. I’m sorry to say that without the use of extensionmethods and lambda expressions, the examples <strong>in</strong> this section are likely to be a littlecontrived, but there’s a dist<strong>in</strong>ct chicken-and-egg situation here: anonymous types aremost useful with<strong>in</strong> the context of the more advanced features, but we need to understandthe build<strong>in</strong>g blocks before we can see much of the bigger picture. Stick with it—it will make sense <strong>in</strong> the long run, I promise.Let’s pretend we didn’t have the Person class, and the only properties we caredabout were the name and age. List<strong>in</strong>g 8.4 shows how we could still build objects withthose properties, without ever declar<strong>in</strong>g a type.List<strong>in</strong>g 8.4Creat<strong>in</strong>g objects of an anonymous type with Name and Age propertiesvar tom = new { Name = "Tom", Age = 4 };var holly = new { Name = "Holly", Age = 31 };var jon = new { Name = "Jon", Age = 31 };Console.WriteL<strong>in</strong>e("{0} is {1} years old", jon.Name, jon.Age);As you can tell from list<strong>in</strong>g 8.4, the syntax for <strong>in</strong>itializ<strong>in</strong>g an anonymous type is similarto the object <strong>in</strong>itializers we saw <strong>in</strong> section 8.3.2—it’s just that the name of the type ismiss<strong>in</strong>g between new and the open<strong>in</strong>g brace. We’re us<strong>in</strong>g implicitly typed local variablesbecause that’s all we can use—we don’t have a type name to declare the variablewith. As you can see from the last l<strong>in</strong>e, the type has properties for the Name and Age,both of which can be read and which will have the values specified <strong>in</strong> the anonymousobject <strong>in</strong>itializer used to create the <strong>in</strong>stance—so <strong>in</strong> this case the output is “Jon is 31 yearsold.” The properties have the same types as the expressions <strong>in</strong> the <strong>in</strong>itializers—str<strong>in</strong>gfor Name, and <strong>in</strong>t for Age. Just as <strong>in</strong> normal object <strong>in</strong>itializers, the expressions used <strong>in</strong>anonymous object <strong>in</strong>itializers can call methods or constructors, fetch properties, performcalculations—whatever you need to do.You may now be start<strong>in</strong>g to see why implicitly typed arrays are important. Supposewe want to create an array conta<strong>in</strong><strong>in</strong>g the whole family, and then iterate through it towork out the total age. List<strong>in</strong>g 8.5 does just that—and demonstrates a few other <strong>in</strong>terest<strong>in</strong>gfeatures of anonymous types at the same time.List<strong>in</strong>g 8.5var family = new[]{Populat<strong>in</strong>g an array us<strong>in</strong>g anonymous types and then f<strong>in</strong>d<strong>in</strong>g the total ageBUses an implicitly typed array <strong>in</strong>itializerLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!