Reputation: 18746
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.
Upvotes: 1
Views: 114
Reputation: 10350
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());
}
}
Upvotes: 2