12

I have a series of extension methods defined for various classes in a C# library. I'm currently writing some F# code and instead of rewriting that code I would simply like to use my existing extension methods in my F# code.

I have added a reference to the library and used the open statement to import the namespace, but the extension methods to not appear in F#

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
TimothyP
  • 21,178
  • 26
  • 94
  • 142

2 Answers2

19

Update:

In the current version of F#, you can simply consume extension methods by adding

open TheNamespaceOfExtensionMethod

to your source file (just like you do in C# with a using directive) and simply call the extension method as if it was a normal instance method.

By the way, you can always call them directly just like a normal static method and pass the object reference as the first parameter without any directives if you want. An extension method is simply a static method decorated with ExtensionAttribute under the hood.

Original answer (before F# 2010 beta; not true anymore, as Dykam points out):

I don't think extension methods are supported by F#. You can always call them directly just like a normal static method and pass the object reference as the first parameter.

Community
  • 1
  • 1
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 6
    +1. Exactly, extension methods are a language feature, not a CLR feature. They are really syntactic sugar to make certain kinds of static method look like instance methods. – Steve Apr 23 '09 at 11:09
9

Being able to 'import' C#/VB extension methods will be supported in the next release of F#.

See also

F# extension methods in C#

for more detail on the topic.

Community
  • 1
  • 1
Brian
  • 117,631
  • 17
  • 236
  • 300