Readers should be aware that the syntax for anonymous methods has changed
somewhat since that whitepaper was posted. Specifically, one no longer uses the
name of the delegate type when declaring the anonymous method.
For example, this code excerpted from the aforementioned whitepaper:
public
MyForm()
{
listBox = new
ListBox(...);
textBox = new
TextBox(...);
button = new
Button(...);
button.Click += new EventHandler(sender,
e)
{
listBox.Items.Add(textBox.Text);
};
}
}
would now be written like this:
public
MyForm()
{
listBox = new
ListBox(...);
textBox = new
TextBox(...);
button = new
Button(...);
button.Click += delegate(object sender, EventArgs
e)
{
listBox.Items.Add(textBox.Text);
};
}
}
It's also worth noting that this example illustrates two new features. The
obvious one is the anonymous method feature. The more subtle feature is the fact
that Whidbey C# relaxes the requirement to explicitly instantiate a delegate
type. For example, consider the first example (not using anonymous
methods):
public
MyForm()
{
listBox = new
ListBox(...);
textBox = new
TextBox(...);
button = new
Button(...);
button.Click += new EventHandler(AddClick);
}
}
In whidbey C#, you are allowed to (but not required to) omit the "new T"
and just do the assignment:
public
MyForm()
{
listBox = new
ListBox(...);
textBox = new
TextBox(...);
button = new
Button(...);
button.Click += AddClick;
}
}
Shades of C function pointers!
Of course, readers should be aware that the moment after I click the submit
button all of this might change yet again (unlikely but not impossible).
As always, consult the actual bits (which can be had at PDC in
October).