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.

Anonymous types225new { Name = "Holly", Age = 31 },new { Name = "Jon", Age = 31 },new { Name = "Tom", Age = 4 },new { Name = "Rob<strong>in</strong>", Age = 1 },new { Name = "William", Age = 1 }};<strong>in</strong>t totalAge = 0;Uses implicitforeach (var person <strong>in</strong> family)typ<strong>in</strong>g for person{totalAge += person.Age; E Sums ages}Console.WriteL<strong>in</strong>e("Total age: {0}", totalAge);Putt<strong>in</strong>g together list<strong>in</strong>g 8.5 and what we learned about implicitly typed arrays <strong>in</strong> section8.4, we can deduce someth<strong>in</strong>g very important: all the people <strong>in</strong> the family are of thesame type. If each use of an anonymous object <strong>in</strong>itializer <strong>in</strong> C created a new type, therewouldn’t be any appropriate type for the array declared at B. With<strong>in</strong> any given assembly,the compiler treats two anonymous object <strong>in</strong>itializers as the same type if there arethe same number of properties, with the same names and types, and they appear <strong>in</strong>the same order. In other words, if we swapped the Name and Age properties <strong>in</strong> one ofthe <strong>in</strong>itializers, there’d be two different types <strong>in</strong>volved—likewise if we <strong>in</strong>troduced anextra property <strong>in</strong> one l<strong>in</strong>e, or used a long <strong>in</strong>stead of an <strong>in</strong>t for the age of one person,another anonymous type would have been <strong>in</strong>troduced.NOTEImplementation detail: how many types?—If you ever decide to look at the IL(or decompiled <strong>C#</strong>) for an anonymous type, be aware that although twoanonymous object <strong>in</strong>itializers with the same property names <strong>in</strong> the sameorder but us<strong>in</strong>g different property types will produce two different types,they’ll actually be generated from a s<strong>in</strong>gle generic type. The generic typeis parameterized, but the closed, constructed types will be different becausethey’ll be given different type arguments for the different <strong>in</strong>itializers.Notice that we’re able to use a foreach statement to iterate over the array just as wewould any other collection. The type <strong>in</strong>volved is <strong>in</strong>ferred D, and the type of theperson variable is the same anonymous type we’ve used <strong>in</strong> the array. Aga<strong>in</strong>, we canuse the same variable for different <strong>in</strong>stances because they’re all of the same type.List<strong>in</strong>g 8.5 also proves that the Age property really is strongly typed as an <strong>in</strong>t—otherwise try<strong>in</strong>g to sum the ages E wouldn’tcompile. The compiler knows about the anonymoustype, and Visual Studio 2008 is evenwill<strong>in</strong>g to share the <strong>in</strong>formation via tooltips,just <strong>in</strong> case you’re uncerta<strong>in</strong>. Figure 8.3 showsthe result of hover<strong>in</strong>g over the person part ofthe person.Age expression from list<strong>in</strong>g 8.5.Now that we’ve seen anonymous types <strong>in</strong>action, let’s go back and look at what the compileris actually do<strong>in</strong>g for us.CDUses sameanonymous typefive timesFigure 8.3 Hover<strong>in</strong>g over a variablethat is declared (implicitly) to be of ananonymous type shows the details ofthat anonymous type.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!