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.

218 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compiler8.3.3 Sett<strong>in</strong>g properties on embedded objectsSo far we’ve found it easy to set the Name and Age properties, but we can’t set the Homeproperty <strong>in</strong> the same way—it’s read-only. However, we can set the town and the countryof a person, by first fetch<strong>in</strong>g the Home property, and then sett<strong>in</strong>g properties on theresult. The language specification refers to this as sett<strong>in</strong>g the properties of an embeddedobject. Just to make it clear, what we’re talk<strong>in</strong>g about is the follow<strong>in</strong>g <strong>C#</strong> 1 code:Person tom = new Person("Tom");tom.Age = 4;tom.Home.Country = "UK";tom.Home.Town = "Read<strong>in</strong>g";When we’re populat<strong>in</strong>g the home location, each statement is do<strong>in</strong>g a get to retrievethe Location <strong>in</strong>stance, and then a set on the relevant property on that <strong>in</strong>stance.There’s noth<strong>in</strong>g new <strong>in</strong> that, but it’s worth slow<strong>in</strong>g your m<strong>in</strong>d down to look at it carefully;otherwise, it’s easy to miss what’s go<strong>in</strong>g on beh<strong>in</strong>d the scenes.<strong>C#</strong> 3 allows all of this to be done <strong>in</strong> one expression, as shown here:Looks like anassignment toHome, but it’snot really!Person tom = new Person("Tom"){Age = 4,Home = { Country="UK", Town="Read<strong>in</strong>g" }};The compiled code for these snippets is effectively the same. The compilerspots that to the right side of the = sign is another object <strong>in</strong>itializer,and applies the properties to the embedded object appropriately. One po<strong>in</strong>t aboutthe formatt<strong>in</strong>g I’ve used—just as <strong>in</strong> almost all <strong>C#</strong> features, it’s whitespace <strong>in</strong>dependent:you can collapse the whitespace <strong>in</strong> the object <strong>in</strong>itializer, putt<strong>in</strong>g it all on one l<strong>in</strong>eif you like. It’s up to you to work out where the sweet spot is <strong>in</strong> balanc<strong>in</strong>g long l<strong>in</strong>esaga<strong>in</strong>st lots of l<strong>in</strong>es.The absence of the new keyword <strong>in</strong> the part <strong>in</strong>itializ<strong>in</strong>g Home is significant. If youneed to work out where the compiler is go<strong>in</strong>g to create new objects and where it’sgo<strong>in</strong>g to set properties on exist<strong>in</strong>g ones, look for occurrences of new <strong>in</strong> the <strong>in</strong>itializer.Every time a new object is created, the new keyword appears somewhere.We’ve dealt with the Home property—but what about Tom’s friends? There areproperties we can set on a List, but none of them will add entries to the list.It’s time for the next feature—collection <strong>in</strong>itializers.8.3.4 Collection <strong>in</strong>itializersCreat<strong>in</strong>g a collection with some <strong>in</strong>itial values is an extremely common task. Until <strong>C#</strong> 3arrived, the only language feature that gave any assistance was array creation—andeven that was clumsy <strong>in</strong> many situations. <strong>C#</strong> 3 has collection <strong>in</strong>itializers, which allow youto use the same type of syntax as array <strong>in</strong>itializers but with arbitrary collections andmore flexibility.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!