1) Add a reference FSharp.Core dll in your project
2) Reference Microsoft.FSharp.Core namespace in your code
using FS = Microsoft.FSharp.Core;
3) Implement your function, for example:
public static IEnumerable<U> map<T, U>(FS.FSharpFunc<T, U> mapper,
IEnumerable<T> input)
{
foreach (T el in input)
{
yield return mapper.Invoke(el);
}
}
Note: there are differences depending on the version of the .Net runtime
(4.0 vs. 3.5)
Bonus: return F# functions from C# code.
You can also implement F# functions in C# code and pass them to F#
public class ReturnString: FSharpFunc<Unit, string>
{
public FSharpFunc<Unit,string> asFSharpFunc()
{
return this;
}
public override string Invoke(Unit unitVar0)
{
return "a string";
}
}
In F# do:
let funcFromCSharp = (ReturnString()).asFSharpFunc()
funcFromCSharp ()
or this
let inline toFunc<'a,'b,'c when 'c :> FSharpFunc<'a,'b>> (x: 'c ): ('a -> 'b) = (# "" x : 'a -> 'b #)
let funcFromCSharp = ReturnString()|> toFunc
funcFromCSharp ()