Call F # function from C #

I have looked at other instructions and still do not know how to do this. I have two projects (Calculator in C # and Logic in F #). I added a link to Logic in the calculator, as well as a link to FSharp.Core

However, when I add the line

float result = Logic.start(formula); 

In my C # project, I get an error:

"The name Logic does not exist in the current context."

There is a Logic module in a logical project, so should it display correctly? What am I missing yet?

EDIT: Here's the function definition ...

 let start formula = core (List.ofSeq formula) [] [] 
+7
c # visual-studio f #
source share
1 answer

You cannot refer to C # only on the F # module without any namespace. Do something like this:

 // F# project namespace Some module Logic = let start formula = ..... 

or equivalent

 // F# project module Some.Logic let start formula = ..... 

and

 // C# project ..... Some.Logic.start(formula) 

and the F # project link from the C # project.

UPDATE As JackP noted, there is another alternative that avoids using an explicit namespace on the F # side in general.

When you create a class in C # outside of any namespace, that class can be passed by adding its name with the global contextual keyword, followed by the :: operator, which is a way of referencing the standard top-level .NET namespace . A F # module with a simple name outside of any namespace from a reference point is equivalent to the one that has namespace global in the very first line of code. Applying this consideration to your case, you can also:

 // F# definition // namespace global is assumed module Logic let start formula = ..... // C# reference ...global::Logic.start(formula)... 

global:: available on MSDN .

+18
source share

All Articles