Alex Rivera | Logout

Call F# code from C#

Asked 2009-01-25T23:40:28.913
82

I am playing around with F# and C#, and would like to call F# code from C#.

I managed to get it to work the other way around in Visual Studio by having two projects in the same solution, and adding a reference of the C# code to the F# project. After doing this, I could call C# code and even step through it while debugging.

What I am trying to do is F# code FROM C# instead of C# code from F#. I added a reference to the F# project to the C# project, but it isn't working the way it did before. I would like to know if this is possible without doing it manually.

Edit
Report

1 Answer

6

From this link they seem to have a number of possible solutions, but the one that seemed the simplest was a comment:

F# Code:

type FCallback = delegate of int*int -> int;;
type FCallback =
  delegate of int * int -> int

let f3 (f:FCallback) a b = f.Invoke(a,b);;
val f3 : FCallback -> int -> int -> int

C# Code:

int a = Module1.f3(Module1.f2, 10, 20); // method gets converted to the delegate automatically in C#
answered 2009-01-25T23:49:17.007

Your Answer