Errata

Programming C# 3.0

Errata for Programming C# 3.0, Fifth Edition

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Printed
Page 73
Section entitled "The ICloneable Interface"

Use of the ICloneable interface is no longer recommended.

Anonymous   
Printed
Page 73
The section entitled "The ICloneable Interface"

Immediately following the section entitled "The ICloneable Interface," please insert the following
section:

Object Initializer

An alternative to calling the constructor to initialize the private member values of the Time class is to
use a new feature in C# 3.0 called the object initializer. The object initializer allows you to
initialize objects without explicitly using a constructor. In fact, you need not even have defined a
constructor that initializes the object properties. All you need to do is to specify the value of each
public property in the object initializer as shown in Example 4-5.

Example 4-5. Object Initializer

using System;

namespace ObjectInitializer
{
public class Time
{
// private member variables
public int Year;
public int Month;
public int Date;
public int Hour;
public int Minute;
public int Second = 30; // initializer

public void DisplayCurrentTime()
{
System.Console.WriteLine("Time : {0}/{1}/{2} {3}:{4}:{5}",
Month, Date, Year, Hour, Minute, Second);
}

}

public class Tester
{
static void Main()
{
Time t = new Time {
Year = 2009, Month = 7, Date = 10, Hour = 11, Minute = 15 };
t.DisplayCurrentTime();
}
}
}

Output:
Time : 7/10/2009 11:15:30
The following object initialization:
Time t = new Time {
Year = 2009, Month = 7, Date = 10, Hour = 11, Minute = 15 };
Is the equivalent of creating the object
Time t = new Time();
and then assigning values to its public members:
t.Year = 200;
t.Month = 7;
t.Date = 10;
t.Hour = 11;
t.Minute = 15;
The member values Year, Month, Date, Hour, and Minute would normally be declared private, and made public
through properties, as described later in this chapter.

Anonymous   
Printed
Page 96
Insert the following section immediately before the section entitled "readOnly fields."

Automatic Properties

It is common to write something like the following (often repeatedly) in your classes:

private int hour;

public int Hour

{
get { return hour; }
set { hour = value; }
}

Recognizing the frequency of this pattern, the C# 3.0 compiler now allows you to write the following, as
a shortcut for the previous code:

public int Hour { get; set; }

The effect is exactly the same: the compiler generates the same underlying code for you. You are free at
any time to fill in the accessors and the backing variable yourself, but this shortcut saves many hours
of coding and is wonderfully convenient.

Note that you can only use automatic properties when you are willing to have both a get and a set
accessor, and when the simple implementation (as shown) is sufficient. There is a work around if you want
only a get or only a set accessor: You can mark either one as private. Thus you can create a read-only
property by writing:

public int Hour { get; private set; }

And you can create a write-only property by writing:
public int Hour { private get; set; }

If you need to use attributes, or if you in any other way modify the simplicity of the backing variable
or the accessors, you must use the normal property syntax.

Anonymous   
Printed
Page 354
14th line of code from bottom

replace the line dc.Customers.Add(douglas); (14th from the bottom) with ?
dc.Customers.InsertOnSubmit(douglas);

Anonymous   
Printed
Page 357
3rd paragraph

Revise paragraph three to read:

With all three new objects created, you can add them to the database just by adding the new Customer
object to the Customers table using the InsertOnSubmit() method, and then telling the DataContext to
submit the changes:.

Anonymous   
Printed
Page 357
Code snippet following paragraph three

In the code snippet that follows the third paragraph, replace the line dc.Customers.Add(douglas);? with
dc.Customers.InsertOnSubmit(douglas);

Anonymous