November 24, 2014
Hot Topics:

What Anonymous Methods Might Look Like in VB.NET

  • June 23, 2005
  • By Paul Kimmel
  • Send Email »
  • More Articles »

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, since VB is getting everything elselike generics and overloaded operatorsI 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.)





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel