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.

226 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compiler8.5.2 Members of anonymous typesAnonymous types are created by the compiler and <strong>in</strong>cluded <strong>in</strong> the compiled assembly<strong>in</strong> the same way as the extra types for anonymous methods and iterator blocks. The CLRtreats them as perfectly ord<strong>in</strong>ary types, and so they are—if you later move from an anonymoustype to a normal, manually coded type with the behavior described <strong>in</strong> this section,you shouldn’t see anyth<strong>in</strong>g change. Anonymous types conta<strong>in</strong> the follow<strong>in</strong>g members:■ A constructor tak<strong>in</strong>g all the <strong>in</strong>itialization values. The parameters are <strong>in</strong> thesame order as they were specified <strong>in</strong> the anonymous object <strong>in</strong>itializer, and havethe same names and types.■ Public read-only properties.■ Private read-only fields back<strong>in</strong>g the properties.■ Overrides for Equals, GetHashCode, and ToStr<strong>in</strong>g.That’s it—there are no implemented <strong>in</strong>terfaces, no clon<strong>in</strong>g or serializationcapabilities—just a constructor, some properties and the normal methods from object.The constructor and the properties do the obvious th<strong>in</strong>gs. Equality between two<strong>in</strong>stances of the same anonymous type is determ<strong>in</strong>ed <strong>in</strong> the natural manner, compar<strong>in</strong>geach property value <strong>in</strong> turn us<strong>in</strong>g the property type’s Equals method. The hashcode generation is similar, call<strong>in</strong>g GetHashCode on each property value <strong>in</strong> turn andcomb<strong>in</strong><strong>in</strong>g the results. The exact method for comb<strong>in</strong><strong>in</strong>g the various hash codestogether to form one “composite” hash is unspecified, and you shouldn’t write codethat depends on it anyway—all you need to be confident <strong>in</strong> is that two equal <strong>in</strong>stanceswill return the same hash, and two unequal <strong>in</strong>stances will probably return differenthashes. All of this only works if the Equals and GetHashCode implementations of allthe different types <strong>in</strong>volved as properties conform to the normal rules, of course.Note that because the properties are read-only, all anonymous types are immutableso long as the types used for their properties are immutable. This provides you with allthe normal benefits of immutability—be<strong>in</strong>g able to pass values to methods withoutfear of them chang<strong>in</strong>g, simple shar<strong>in</strong>g of data across threads, and so forth.We’re almost done with anonymous types now. However, there’s one slight wr<strong>in</strong>klestill to talk about—a shortcut for a situation that is fairly common <strong>in</strong> LINQ.8.5.3 Projection <strong>in</strong>itializersThe anonymous object <strong>in</strong>itializers we’ve seen so far have all been lists of name/valuepairs—Name = "Jon", Age=31 and the like. As it happens, I’ve always used constantsbecause they make for smaller examples, but <strong>in</strong> real code you often want to copy propertiesfrom an exist<strong>in</strong>g object. Sometimes you’ll want to manipulate the values <strong>in</strong> someway, but often a straight copy is enough.Aga<strong>in</strong>, without LINQ it’s hard to give conv<strong>in</strong>c<strong>in</strong>g examples of this, but let’s go backto our Person class, and just suppose we had a good reason to want to convert a collectionof Person <strong>in</strong>stances <strong>in</strong>to a similar collection where each element has just a name,and a flag to say whether or not that person is an adult. Given an appropriate personvariable, we could use someth<strong>in</strong>g like this:new { Name = person.Name, IsAdult = (person.Age >= 18) }Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!