Previous month:
August 2005
Next month:
October 2005

September 2005

Lambda Expressions and Anonymous Methods

C# 3.0 enhances anonymous methods that are new with C# 2.0: lambda expressions. To show the concept of anonymous methods and lambda expressions let's look at this simple extension method DisplaySelected that writes selected properties to the console. Because the method is defined as a generic method, it can work with every IEnumerable<T> collection. The implementation includes a simple foreach statement where every element from the source collection is written to the console. What information from the element is written to the console is defined by the selector.

   public static IEnumerable<T> DisplaySelected<T, TSelector>(
this IEnumerable<T> source, Func<T, TSelector> selector)
      foreach (T element in source)
      return source;

The second parameter of the extension method is selector of type Func<T, TSelector>. Func is defined as this delegate:

public delegate T Func<A0, T>(A0 arg0);

To first demonstrate invoking the method DisplaySelected, I'm defining the method GetFirstname where the type T for the Func delegate. GetFirstname returns the firstname of the passed object, so that if used with DisplaySelected, the firstname of a person is written to the console.

static string GetFirstname(Person p)
   return p.Firstname;

Now we have different ways to invoke the method DisplaySelected.

persons is an object of type List<Person>.

A traditional way to invoke the method DisplaySelected is by instantiating the delegate Func, and passing the address of GetFirstname:

   persons.DisplaySelected<Person, string>(new Func<Person, string>(GetFirstname));

C# 2.0 allows inferring the delegate. So instead of instantiating a new delegate Func, the method name can be directly passed to DisplaySelected. The compiler creates the same code to instantiate the delegate.

persons.DisplaySelected<Person, string>(GetFirstname);

Instead of using the method GetFirstname, C# 2.0 allows a anonymous method syntax where the implementation of the method that is invoked by the Func delegate can be directly written as the parameter.

persons.DisplaySelected<Person, string>(delegate(Person p) {return p.firstname;});

C# 3.0 allows a new syntax for lambda expressions. Lambda expressions are written as a parameter list (p), followed by the => token and an expression or statement block.

persons.DisplaySelected<Person, string>(p => p.firstname);

Because the generic types of DisplaySelected can be inferred that way, the method call can also be written in this simple form: 

persons.DisplaySelected(p => p.firstname);

Lambda expressions offer great possibilities :-)


Extension Methods

C# 3.0 makes it possible to add methods to existing classes that are defined in other assemblies.

All the extension methods must be declared static. Extension methods are made by using the this keyword with the first parameter. Here I'm creating a Foo extension for the String class. These parameters can also be interfaces and generics to make it more flexible :-)

public static class MyExtension
   public static void Foo(this string s)
      Console.WriteLine("extension method Foo invoked {0}", s);

Now it's possible to invoke the Foo method like an instance method with a string. The string is passed with the first parameter of the method.

string s = "extensions are cool";

In some future articles I will show some cool uses of this feature.

One important note from the C# specs:
Extension methods are less discoverable and more limited in functionality than instance methods. For those reasons, it is recommended that extension methods be used sparingly and only in situations where instance methods are not feasible or possible.



Have you been looking for VSA the? Some years ago VSA (Visual Studio for Applications) was announced as a replacement for VBA, for including in custom applications. Lately it has been quiet about VSA. There's a good reason: VSTA - Visual Studio Tools for Applications.

VSTA makes it simple to customize applications. The Wednesday keynote at PDC included a AutoCAD demonstration. AutoCAD has the VSTA IDE embedded to provider customization. Starting Visual Studio from within AutoCAD creates a project and offers helper classes. From here the .NET Framework is available to invoke Web services, get to other data....


Windows Workflow Foundation

Did you miss something like activity diagrams with Visual Studio? Visual Studio 2005 includes some cool designers such as a class designer, application designer, system designer... There's a good reason an activity diagram is missing. A diagram that can be compared is coming with Windows Workflow Foundation (WWF).

WWF is a .NET class library that includes support for both system and human workflow. Of course the designer doesn't look like UML activity diagrams, as here code gets generated. In the Wednesday keynote at PDC it was shown to define a breakpoint in the designer. From therer it is possible to step into the code. Cool!

Office 12 and the next version of BizTalk server are using WWF themselves.


Office 12

Office 12 was one of the products that was demonstrated at Bill Gates PDC keynote.

When users have been asked about the features they would like to have in the new Office version, 9 out of 10 users asked for features that are already available. So the UI needs a change! The ribbon - no more menus (except the File menu). Instead, there are tabs. With every tab different controls show up depending on the context. The user interface is highly customizable with XML and managed code.

More XML: XML is deeply integrated into Office 12. While Office 2003 supports XML (see Creating Word documents with XML part 1 and part 2, and Getting data from Excel 2003), now the default file format is XML. A docx, pptx and xlsx file is a compressed file that contains XML, pictures, and all what belongs to the document. For backward compatibility, Office 2000 + Office 2003 will support this format, too (if the extensions are installed).


Microsoft Max

At PDC keynote Microsoft Max was shown. Jim Allchin asked a team of six developers to create a sample application using WinFX. The result is Microsoft Max, a cool picture sharing application! This application makes use of WCF for peer-to-peer communication, and WPF for the user interface with some three-dimensional features showing the pictures.

Team blog for Microsoft Max

Microsoft Max Homepage including the download!


WinFX Namespaces

Yesterday evening I arrived in Los Angeles (some hourse later than expected because of a cancelled flight). Sunday morning I checked in at PDC. In the bag there's an interesting poster about the major namespaces of WinFX.

Here I'm skipping the namespaces known from 1.0 to only list the 2.0, 3.0 and Future namespaces.


  • System.Transactions (2.0)
  • System.Collections.Generic (2.0)
  • System.Security.AccessControl (2.0)
  • Managed Add-in Framework
    • System.AddIns (Future)
      • Contract
  • Microsoft.Build (2.0)
  • System.Runtime
    • ConstrainedExecution (2.0)
  • ClickOnce Deployment
    • System.Deployment (2.0)


  • System.Windows (3.0)
    • Annotations, Automation, Controls, Data, Documents, Ink, Interop, Navigation, Serialization, Shapes, Threading
  • System.Windows.Media (3.0)
    • Media3D, Animation, Imaging, TextFormatting, Effects
  • System.Web (2.0 updates)
    • Management (2.0)
  • System.Printing (3.0)
  • System.Speech (3.0)


  • System.Web.Profile (2.0)
  • Language Integrated Query
    • System.Query (Future)
    • System.Data.DLinq (Future)
    • System.Xml.XLinq (Future)
    • System.Expressions (Future)
  • WinFS
    • System.Storage (Future)
      • Core, Audio, Calendar, Contacts, Documents, Image, Media, Messages, Rules, Sync, Video (Future)
  • XPS Documents
    • System.Windows.Xps (3.0)
    • System.IO.Packaging (3.0)


  • Windows Communication Foundation
    • System.ServiceModel (3.0)
      • Channels, Configuration, Diagnostics, Integration
    • System.ServiceModel.Security (3.0)
      • Protocols, Tokens
  • Network Class Library
    • System.Net
      • Mail (2.0)
      • NetworkInformation (2.0)
      • Security (2.0)
  • Windows Workflow Foundation
    • System.Workflow (3.0)
    • System.Workflow.Activities (3.0)
    • System.Workflow.ComponentModel (3.0)
      • Compiler, Design, Serialization
    • System.Workflow.Runtime (3.0)
      • Hosting, Messaging
  • Identity Management
    • System.InfoCards (3.0)

Update: Brad Abrams has a PDF version of this poster!