Assignments

Assignments are generally considered to be statements in the first place, even though we discussed them in the context of expressions before. In 99 percent of cases, assignments will take a statement form:

name = "Bart";age = 25;age += 1;

In Chapter 5, you saw how assignment expressions take on the value that is assigned to the left side. Although this is definitely a nice property, it should be used sparingly. One of the few legitimate uses I can think of is the type switch mentioned in Chapter 6, “A Primer on Types and Objects,” the section, “Intermezzo: The Mythical Type Switch.”

if ((f = item as Fruit) != null){    // Do something with the juicy piece of fruit...}

Because you already know everything ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.