Prev | Current Page 390 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


The fact that there are no CLR changes for .NET 3.5 means that almost all of the
new features exposed in C# 3 are due to the compiler being willing to do more work
for you. We saw some evidence of this in C# 2??”particularly with anonymous methods
and iterator blocks??”and C# 3 continues in the same vein. In this chapter, we??™ll meet
the following features that are new to C# 3:
?–  Automatically implemented properties??”Removing the drudgery of writing simple
properties backed directly by fields.
?–  Implicitly typed local variables??”When you declare a variable and immediately
assign a value to it, you no longer need to specify the type in the declaration.
?–  Object and collection initializers??”Simple ways to initialize objects in single expressions.
?–  Implicitly typed arrays??”Let the compiler work out the type of new arrays, based
on their contents.
?–  Anonymous types??”Primarily used in LINQ, these allow you to create new ???ad
hoc??? types to contain simple properties.
As well as describing what the new features do, I??™ll make recommendations about their
use. Many of the features of C# 3 require a certain amount of discretion and restraint
on the part of the developer. That??™s not to say they??™re not powerful and incredibly useful
??”quite the reverse??”but the temptation to use the latest and greatest syntactic sugar
shouldn??™t be allowed to overrule the drive toward clear and readable code.


Pages:
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402