In this post, we will see how to resolve Is there a way to run F# code from a C# program?
Question:I use Rider and/or Visual Studio for Mac to run my C# code. I can individually create an F# project if needed. How do I run the F# code within the C# main program:
printf("Hello, World!\n")or any code really
I tried this in the terminal: Terminal VS error
Best Answer:Create an F# class library, add your F# code. Create a C# console (or whatever) app, reference the F# class library, and call the f# code from your C# code. you may need a ‘using’ statement to get your F# code accessible.
C#/F# code both compile to similar binary files (PE code, or something like that), which are mostly accessible from the other language, there are some features of both languages that don’t translate (SRTPs being the most obvious thing not accessible from C#).
Interoperability though is at this binary level, you can’t just write F# code in a C# source file or vice versa (though this is possible in some other languages).
create F# class library called “ClassLibrary1”
in Visual Studio
Add project reference from consoleapp1 to classlibrary1
your C# project file should look something like this (obviously it may be slightly different but the crucial bit is the project reference)
paste this code into your C# code
get that working, then just do the first bit with an F# class library instead of a C# one
If you have better answer, please add a comment about this, thank you!