Game Development Reference
In-Depth Information
XML and databases too. LINQ works with another new addition to C# 3.0:
lambda functions.
Lambda Functions
Remember anonymous delegates? Well, lambda functions are a much more
convenient way to write anonymous delegates. Here's an example that destroys
all items in a player's inventory.
_itemList.ForEach(delegate(Item x) { x.Destroy(); });
Using the lambda functions, it can be written more concisely as follows.
_itemList.ForEach(x ¼> x.Destroy());
Less boilerplate code is required making it easier to read.
Object Initializers
Object initializers allow objects to be constructed in a more flexible manner, and
constructors no longer need to be so verbose. Here's an example without object
initializers.
Spaceship s ¼ new Spaceship();
s.Health ¼ 30;
This can now be written in a more concise way.
Spaceship s ¼ new Spaceship { Health ¼ 30 };
This is used in some LINQ queries. Object initializers can help prevent the need
to write multiple constructors that only set a few fields.
Collection Initializers
Collection initializers let you initialize lists in a pleasing way. As before, the best
way to demonstrate the improvement is to show the old way.
class Orc
{
List < Item > _items ¼ new List < Item > ();
public Orc()
{
_items.Add(new Item(''axe''));
 
Search WWH ::




Custom Search