Datasheet
24
x
CHAPTER 1 PRIMER
Of course, types need not be the only thing inferred by the compiler; because public is the most
common access modifi er for methods, constructors and properties, and
private is most common
for fi elds, let those be the inferred default:
// This is not legal C# 3.0
class Person
{
Person(firstName, lastName, age : int)
{
FirstName = firstName;
LastName = lastName;
Age = age;
}
FirstName { get; set; }
LastName { get; set; }
Age { get; set; }
FullName {
get { return FirstName + “ “ + LastName; }
}
}
While syntax is under the microscope, the constructor syntax is a bit strange — why is repeating
the type’s name necessary? And because most types have a principal constructor to which all other
constructors defer, if it even has multiple constructors at all, that constructor should have a more
prominent place in the type’s declaration:
// This is not legal C# 3.0
class Person(firstName, lastName, age : int)
{
FirstName { get; set; }
LastName { get; set; }
Age { get; set; }
FullName {
get { return FirstName + “ “ + LastName; }
}
}
Problem is, the constructor body is now missing, and the assignment of the constructor parameters
to the respective properties is lost, unless somehow the body of the class can serve as the body of the
constructor, and the property declaration can know how to “line up” against those parameters:
// This is not legal C# 3.0
class Person(firstName, lastName, age : int)
{
FirstName { get { firstName } set; }
LastName { get { lastName } set; }
Age { get { age } set; }
FullName {
get { return FirstName + “ “ + LastName; }
}
}
c01.indd 24c01.indd 24 10/1/2010 3:20:39 PM10/1/2010 3:20:39 PM