views:

95

answers:

1

OK, I've probably misunderstood something here but, as far as I can see ...

  • An extension method has to be contained in a module, not a class
  • You can't make methods in modules Static/Shared
  • Therefore you can't use an extension method on a class without instantiating it.

In other words you can't make an extension method on String called "MyExtensionMethod" and use:

String.MyExtensionMethod("String")

But instead ..

Dim test As String
test.MyExtensionMethod("string")

Is this correct? Or is there a way I can get extension methods to work as static methods?

+4  A: 

You are correct. Extension methods can only act on instances of a type.

And no, unfortunately there's no crafty way to write extension methods that act on the types themselves, behaving like static methods.

LukeH
Thanks for the answer. Seems like an incredible oversight though, since most "helper type" functions on existing framework objects function as static :(
Matt Thrower