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.

Simplified <strong>in</strong>itialization217The part <strong>in</strong> braces at the end of each l<strong>in</strong>e is the object <strong>in</strong>itializer. Aga<strong>in</strong>, it’s just compilertrickery. The IL used to <strong>in</strong>itialize tom3 and tom4 is identical, and <strong>in</strong>deed it’s verynearly 2 the same as we used for tom1. Predictably, the code for tom5 is nearly the sameas for tom2. Note how for tom4 we omitted the parentheses for the constructor. Youcan use this shorthand for types with a parameterless constructor, which is what getscalled <strong>in</strong> the compiled code.After the constructor has been called, the specified properties are set <strong>in</strong> the obviousway. They’re set <strong>in</strong> the order specified <strong>in</strong> the object <strong>in</strong>itializer, and you can onlyspecify any particular property at most once—you can’t set the Name property twice,for example. (You could, however, call the constructor tak<strong>in</strong>g the name as a parameter,and then set the Name property. It would be po<strong>in</strong>tless, but the compiler wouldn’tstop you from do<strong>in</strong>g it.) The expression used as the value for a property can be anyexpression that isn’t itself an assignment—you can call methods, create new objects(potentially us<strong>in</strong>g another object <strong>in</strong>itializer), pretty much anyth<strong>in</strong>g.You may well be wonder<strong>in</strong>g just how useful this is—we’ve saved one orImportant!Oneexpression to<strong>in</strong>itialize anobjecttwo l<strong>in</strong>es of code, but surely that’s not a good enough reason to make thelanguage more complicated, is it? There’s a subtle po<strong>in</strong>t here, though:we’ve not just created an object <strong>in</strong> one l<strong>in</strong>e—we’ve created it <strong>in</strong> oneexpression. That difference can be very important. Suppose you want tocreate an array of type Person[] with some predef<strong>in</strong>ed data <strong>in</strong> it. Evenwithout us<strong>in</strong>g the implicit array typ<strong>in</strong>g we’ll see later, the code is neat andreadable:Person[] family = new Person[]{new Person { Name="Holly", Age=31 },new Person { Name="Jon", Age=31 },new Person { Name="Tom", Age=4 },new Person { Name="William", Age=1 },new Person { Name="Rob<strong>in</strong>", Age=1 }};Now, <strong>in</strong> a simple example like this we could have written a constructor tak<strong>in</strong>g both thename and age as parameters, and <strong>in</strong>itialized the array <strong>in</strong> a similar way <strong>in</strong> <strong>C#</strong> 1 or 2.However, appropriate constructors aren’t always available—and if there are severalconstructor parameters, it’s often not clear which one means what just from the position.By the time a constructor needs to take five or six parameters, I often f<strong>in</strong>d myselfrely<strong>in</strong>g on IntelliSense more than I want to. Us<strong>in</strong>g the property names is a great boonto readability <strong>in</strong> such cases.This form of object <strong>in</strong>itializer is the one you’ll probably use most often. However,there are two other forms—one for sett<strong>in</strong>g subproperties, and one for add<strong>in</strong>g to collections.Let’s look at subproperties—properties of properties—first.2In fact, the variable’s new value isn’t assigned until all the properties have been set. A temporary local variableis used until then. This is very rarely noticeable, though, and where it is the code should probably be morestraightforward anyway.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!