What Anonymous Methods Might Look Like in VB.NET

Anonymous methods are similar to C++ inline methods. Basically, an anonymous method is the method body without the method header defined at the point of use. In .NET, anonymous methods are used for delegates. (Think event handlers.)

Thus far, anonymous methods haven’t shown up in the current beta version of VB.NET 2.0. However, because VB is getting everything else—like generics and overloaded operators—I suspect anonymous methods are on the way too. This article shows how to define anonymous methods in C# and what they might look like in VB.NET.

Present, Basic Ground Rules

Anonymous methods are at present defined as a block of code, cast to a delegate, and assigned to a delegate. Listing 1 shows what a traditional delegate assignment and definition would look like in C#:

Listing 1: A traditional event handler definition and delegate assignment in a stripped down Windows Forms C# 2.0 sample application

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Anonymous_CSharp
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         button1.Click += new EventHandler(ButtonClick);
      }

      private void ButtonClick(object sender, EventArgs e)
      {
         MessageBox.Show("Hello, World!");
      }
   }
}

In VB.NET, you think in terms of event properties, which are the same as delegates. By current definition, you cannot use reference or out arguments in anonymous methods, and they can’t contain unsafe code. Thus, if you compile all of these elements (no ref or out arguments, no unsafe code, no method header, casting to a delegate, and assigning to a delegate property), you get the code in Listing 2 (an anonymous method that yields the same result as Listing 1):

Listing 2: Code that behaves identically to Listing 1 but uses an anonymous delegate method

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Anonymous_CSharp
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         button1.Click += delegate{ MessageBox.Show("Hello, World!"); };
      }

   }
}

If you compare the code in Listing 1 and Listing 2, you can see the stripped down anonymous method in (bold) in Listing 2. This supposedly enhances code reuse (according to the Visual Studio Beta 2 help), but this claim seems dubious at best. Inline code may traditionally have been slightly optimal, but with modern optimizing compilers even that seems uncertain. Another claim made by the help system is that anonymous methods eliminate the need to declare delegates for custom delegates. This is true, but they eliminate only a single line of code per delegate along with the method header.

Anonymous methods are clever, but they may lead to esoterrorism. While fun, they are probably just clever. (Esoterrorism is the state when code is so esoteric that it spreads fear, panic, and confusion to the maintainer.)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read