Datasheet
22
x
CHAPTER 1 PRIMER
written in small chunks, even as small as simple operations and then composed together, such as
what might be needed for input validation code for a web application. However, making this work
in a syntax that doesn’t drive the average C# developer insane (if it hasn’t already) is diffi cult, which
leads to the next question — what if we could somehow make the syntax cleaner and easier to read
and understand?
TYPE INFERENCE
One thing is apparent from all this, particularly the defi nition of the Curry method: This is heavily
genericized code, and it’s not easy to read. It’s a long way from
List<T>! Fortunately, C# 3.0 intro-
duced anonymous local variables, effectively informing the compiler that it is now responsible for
determining the type of the declared local variable:
var add9 = Curry(add5);
int result9 = add9(2)(3);
Here, the compiler uses the context surrounding the variable declaration to fi gure out, at compile-
time, precisely what the type of the local variable should be. In other words, despite the vague-
sounding
var syntax, this is still a full statically-typed variable declaration — it’s just that the
programmer didn’t have to make the declaration explicit because the compiler could fi gure it out
instead.
Theoretically, this means now that the compiler can remove responsibility for the “physical details”
about the code from the programmers’ shoulders:
var x = 2;
var y = 3;
var add10 = add9(x)(y);
Are x and y local variables, or are they property declarations? If these were members of a class,
would they be fi elds or properties or even methods? Is
add10 a method or delegate? More important,
does the programmer even care? (In all but a few scenarios, probably not.)
Ideally, this is something that could be layered throughout the language — such that fi elds,
properties, method parameters, and more, and could all be inferred based on context and usage,
such as:
// This is not legal C# 3.0
class Person
{
public Person(var firstName, var lastName, var age)
{
FirstName = firstName;
LastName = lastName;
Age = age;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string FullName {
get { return FirstName + “ “ + LastName; }
}
}
c01.indd 22c01.indd 22 10/1/2010 3:20:38 PM10/1/2010 3:20:38 PM










