1

I know you can define empty namespace extension methods in c#, can you define the same methods in F# for consumption by C#? I followed this general post on getting C# compatible extension methods, but I'm not having any luck figuring out the final step for a 1-1 conversion.

this is not a duplicate, I'm looking for the way to define extension methods that are in the global namespace as far as C# is concerned, not how to define regular extension methods in F# that C# can see.

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • possible duplicate of [F# extension methods in C#](http://stackoverflow.com/questions/702256/f-extension-methods-in-c-sharp) – GregRos Mar 15 '15 at 02:11
  • This is a duplicate of http://stackoverflow.com/questions/702256/f-extension-methods-in-c-sharp. If you're asking whether the same extension method you define in F# can be visible in C#, the answer is a definite no. The way the languages handle extensions is completely different. – GregRos Mar 15 '15 at 02:12

1 Answers1

2

You definitely can create such extension methods in F#. This may be achieved via Global Namespace exploiting the feature that an F# module declared outside of any namespace gets into this default global namespace as in the snippets below:

// Tested under Visual Studio 2013 Premium/F# 3.1:

// F# library module
[<System.Runtime.CompilerServices.Extension>]
module Extension
    [<System.Runtime.CompilerServices.Extension>]
    let Superb(s: System.String) = "Superb!!!"

and

// Use extension method on System.String from C# console app:
class Test
{
    static void Main(string[] args)
    {
        System.Console.WriteLine("abc".Superb());
    }
}
Gene Belitski
  • 10,270
  • 1
  • 34
  • 54