15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

anonymous Types ❘ 79<br />

As noted earlier, date is represented by the class System.DateTime. The following code uses a System.<br />

DateTime constructor that takes three parameters (the year, month, <strong>and</strong> day of the month — you can find<br />

details of this <strong>and</strong> other DateTime constructors in the MSDN documentation):<br />

public class Document<br />

{<br />

public readonly DateTime CreationDate;<br />

public Document()<br />

{<br />

// Read in creation date from file. Assume result is 1 Jan 2002<br />

// but in general this can be different for different instances<br />

// of the class<br />

CreationDate = new DateTime(2002, 1, 1);<br />

}<br />

}<br />

CreationDate <strong>and</strong> MaxDocuments in the previous code snippet are treated like any other field, except that<br />

because they are read-only, they cannot be assigned outside the constructors:<br />

void SomeMethod()<br />

{<br />

MaxDocuments = 10;<br />

}<br />

// compilation error here. MaxDocuments is readonly<br />

It’s also worth noting that you don’t have to assign a value to a readonly field in a constructor. If you don’t<br />

do so, it will be left with the default value for its particular data type or whatever value you initialized it to<br />

at its declaration. That applies to both static <strong>and</strong> instance readonly fields.<br />

anonymous TyPes<br />

Chapter 2, “Core <strong>C#</strong>” discussed the var keyword in reference to implicitly typed variables. When used<br />

with the new keyword, anonymous types can be created. An anonymous type is simply a nameless class that<br />

inherits from object. The definition of the class is inferred from the initializer, just as with implicitly typed<br />

variables.<br />

If you need an object that contains a person’s first, middle, <strong>and</strong> last name, the declaration would look<br />

like this:<br />

var captain = new {FirstName = "James", MiddleName = "T", LastName = "Kirk"};<br />

This would produce an object with FirstName, MiddleName, <strong>and</strong> LastName properties. If you were to<br />

create another object that looked like this:<br />

var doctor = new {FirstName = "Leonard", MiddleName = "", LastName = "McCoy"};<br />

the types of captain <strong>and</strong> doctor are the same. You could set captain = doctor, for example.<br />

If the values that are being set come from another object, then the initializer can be abbreviated. If you<br />

already have a class that contains the properties FirstName, MiddleName, <strong>and</strong> LastName <strong>and</strong> you have<br />

an instance of that class with the instance name person, then the captain object could be initialized<br />

like this:<br />

var captain = new {person.FirstName, person.MiddleName, person.LastName};<br />

The property names from the person object would be projected to the new object named captain. So the<br />

object named captain would have the FirstName, MiddleName, <strong>and</strong> LastName properties.<br />

The actual type name of these new objects is unknown. The compiler “makes up” a name for the type,<br />

but only the compiler will ever be able to make use of it. So you can’t <strong>and</strong> shouldn’t plan on using any type<br />

reflection on the new objects because you will not get consistent results.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!