Glee. I just got the January CTP of Visual Studio 9.0, with C# 3.0. It's got LINQ: Language Integrated Natural Query.
So, if I want to find all the items on a ToolStrip which are Buttons, are enabled and have their display style set to Image, I used to do this:
C# code:
List<ToolStripButton> buttons = new List<ToolStripButton>;
foreach (ToolStripItem item in toolStrip1.Items)
{
ToolStripButton button = item as ToolStripButton;
if (button != null)
{
if (button.Enabled && button.DisplayStyle == ToolStripItemDisplayStyle.Image)
{
buttons.Add(button);
}
}
}
But now I can do this, instead:
C# code:
var buttons = from button in this.toolStrip1.Items.Cast<ToolStripItem>()
where button is ToolStripButton
&& button.Enabled
&& ((ToolStripButton)button).DisplayStyle == ToolStripItemDisplayStyle.Image
select button;
(That Cast<ToolStripItem> bit won't be necessary in the final release, once they get the type-inference algorithm working properly.)
You can use similar code to "query" XML and relational databases, too. Where's your Delphi now, bitches?
EDITED: 13 Feb 2007 17:12 by RENDLE