Saturday, February 17, 2007

VB 9 Extension Methods Documentation Series

Scott Wisniewski, a developer on the VB 9 Compiler Team has posted the first five parts of a series of articles on the new extension methods included in Visual Basic 9.0's "Orcas" release.

Series topics are:

Here's a part of Scott's description of extension methods:

[Extension methods] provide a very powerful and elegant way to inject your own custom functionality into other people's types. In particular, through a great deal of compiler magic, Extension methods enable you to create a method in a module decorated with an attribute and have that method appear as if it was an instance method defined on another type.

Extension Methods and LINQ

Extension methods play a major role in LINQ. According to Don Box and Anders Hejlsberg in "The LINQ Project" (September 2005):

Extension methods combine the flexibility of "duck typing" made popular in dynamic languages with the performance and compile-time validation of statically typed languages. With extension methods third parties may augment the public contract of a type with new methods while still allowing individual type authors to provide their own specialized implementation of those methods.

Extension methods are defined in static classes as static methods, but are marked with the [System.Runtime.CompilerServices.Extension] attribute in CLR metadata. Languages are encouraged to provide a direct syntax for extension methods. ...

Extension methods are resolved at compile-time based on which extension methods are in scope. When a namespace is imported with C#'s using statement or VB's Import statement, all extension methods that are defined by static classes from that namespace are brought into scope.

The standard query operators are defined as extension methods in the type System.Query.Sequence. When examining the standard query operators, you'll notice that all but one of them is defined in terms of the IEnumerable<T> interface (the exception is OfType, which is described later). This means that every IEnumerable<T>-compatible information source gets the standard query operators simply by adding the using System.Query statement in C# [or Imports System.Query in VB].

Most extension method examples are written in C#. A serious exposition of VB's forthcoming extension methods is most welcome.

As usual, I'll update this post as Scott adds parts to his series.

Technorati tags: , , ,

0 comments: