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.

220 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compiler{"Jon", 31},{"Tom", 4}};In this case, the Add(str<strong>in</strong>g, <strong>in</strong>t) method would be called three times. If multipleAdd overloads are available, different elements of the <strong>in</strong>itializer can call different overloads.If no compatible overload is available for a specified element, the code will failto compile. There are two <strong>in</strong>terest<strong>in</strong>g po<strong>in</strong>ts about the design decision here:■■The fact that the type has to implement IEnumerable is never used by the compiler.The Add method is only found by name—there’s no <strong>in</strong>terface requirementspecify<strong>in</strong>g it.These are both pragmatic decisions. Requir<strong>in</strong>g IEnumerable to be implemented is areasonable attempt to check that the type really is a collection of some sort, and us<strong>in</strong>gany public overload of the Add method (rather than requir<strong>in</strong>g an exact signature)allows for simple <strong>in</strong>itializations such as the earlier dictionary example. Nonpublicoverloads, <strong>in</strong>clud<strong>in</strong>g those that explicitly implement an <strong>in</strong>terface, are not used. This isa slightly different situation from object <strong>in</strong>itializers sett<strong>in</strong>g properties, where <strong>in</strong>ternalproperties are available too (with<strong>in</strong> the same assembly, of course).An early draft of the specification required ICollection to be implemented<strong>in</strong>stead, and the implementation of the s<strong>in</strong>gle-parameter Add method (as specifiedby the <strong>in</strong>terface) was called rather than allow<strong>in</strong>g different overloads. Thissounds more “pure,” but there are far more types that implement IEnumerable thanICollection—and us<strong>in</strong>g the s<strong>in</strong>gle-parameter Add method would be <strong>in</strong>convenient.For example, <strong>in</strong> our case it would have forced us to explicitly create an<strong>in</strong>stance of a KeyValuePair for each element of the <strong>in</strong>itializer. Sacrific<strong>in</strong>ga bit of academic purity has made the language far more useful <strong>in</strong> real life.POPULATING COLLECTIONS WITHIN OTHER OBJECT INITIALIZERSSo far we’ve only seen collection <strong>in</strong>itializers used <strong>in</strong> a stand-alone fashion to createwhole new collections. They can also be comb<strong>in</strong>ed with object <strong>in</strong>itializers to populateembedded collections. To show this, we’ll go back to our Person example. TheFriends property is read-only, so we can’t create a new collection and specify that asthe collection of friends—but we can add to whatever collection is returned by theproperty’s getter. The way we do this is similar to the syntax we’ve already seen for sett<strong>in</strong>gproperties of embedded objects, but we just specify a collection <strong>in</strong>itializer <strong>in</strong>steadof a sequence of properties.Let’s see this <strong>in</strong> action by creat<strong>in</strong>g another Person <strong>in</strong>stance for Tom, this time withfriends (list<strong>in</strong>g 8.3).List<strong>in</strong>g 8.3Build<strong>in</strong>g up a rich object us<strong>in</strong>g object and collection <strong>in</strong>itializersPerson tom = new Person{Name = "Tom",Age = 4,Calls parameterless constructorSets propertiesdirectlyLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!