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.

Simplified <strong>in</strong>itialization215simple reason that code is harder to format <strong>in</strong> pr<strong>in</strong>t than on a screen—there’s not asmuch width available.We’ll come back to implicit typ<strong>in</strong>g when we see anonymous types, as they create situationswhere you are forced to ask the compiler to <strong>in</strong>fer the types of some variables.Before that, let’s have a look at how <strong>C#</strong> 3 makes it easier to construct and populate anew object <strong>in</strong> one expression.8.3 Simplified <strong>in</strong>itializationOne would have thought that object-oriented languages would have streaml<strong>in</strong>edobject creation long ago. After all, before you start us<strong>in</strong>g an object, someth<strong>in</strong>g has tocreate it, whether it’s through your code directly or a factory method of some sort.And yet <strong>in</strong> <strong>C#</strong> 2 very few language features are geared toward mak<strong>in</strong>g life easier whenit comes to <strong>in</strong>itialization. If you can’t do what you want us<strong>in</strong>g constructor arguments,you’re basically out of luck—you need to create the object, then manually <strong>in</strong>itialize itwith property calls and the like.This is particularly annoy<strong>in</strong>g when you want to create a whole bunch of objects <strong>in</strong>one go, such as <strong>in</strong> an array or other collection—without a “s<strong>in</strong>gle expression” way of<strong>in</strong>itializ<strong>in</strong>g an object, you’re forced to either use local variables for temporary manipulation,or create a helper method that performs the appropriate <strong>in</strong>itialization basedon parameters.<strong>C#</strong> 3 comes to the rescue <strong>in</strong> a number of ways, as we’ll see <strong>in</strong> this section.8.3.1 Def<strong>in</strong><strong>in</strong>g our sample typesThe expressions we’re go<strong>in</strong>g to be us<strong>in</strong>g <strong>in</strong> this section are called object <strong>in</strong>itializers.These are just ways of specify<strong>in</strong>g <strong>in</strong>itialization that should occur after an object hasbeen created. You can set properties, set properties of properties (don’t worry, it’s simplerthan it sounds), and add to collections that are accessible via properties. To demonstrateall this, we’ll use a Person class aga<strong>in</strong>. To start with, there’s the name and agewe’ve used before, exposed as writable properties. We’ll provide both a parameterlessconstructor and one that accepts the name as a parameter. We’ll also add a list offriends and the person’s home location, both of which are accessible as read-onlyproperties, but that can still be modified by manipulat<strong>in</strong>g the retrieved objects. A simpleLocation class provides Country and Town properties to represent the person’shome. List<strong>in</strong>g 8.2 shows the complete code for the classes.List<strong>in</strong>g 8.2A fairly simple Person class used for further demonstrationspublic class Person{public <strong>in</strong>t Age { get; set; }public str<strong>in</strong>g Name { get; set; }List friends = new List();public List Friends { get { return friends; } }Location home = new Location();Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!