How to use f# in c# And conversely

Hi, I’m a C# developer, I want to use F# in C# into asp core 3.1 (C#) project. I’m new to F#, I don’t know how to combine these languages together (F# in C# and C# in F#). Does anyone have a short video tutorial on this?

Not a video but you probably could start with this article:

1 Like

I have a sample project of using F# types from C#. Looking for feedback for expanding it. https://github.com/dandereggK/FSharp_Types_In_CSharp

2 Likes
2 Likes

essentially in a .netcore project you can always reference a F# assembly from e.g. C# netcore, usually you would create a F# library project as .netstandard2.0 and reference it from your C# project with project reference.

It’s pretty straightforward!
simplifications:

  • module in F# becomes a static class when accessed by C#, so functions will be accessible as a static classes with static methods.
  • you can decorate your F# records (types) with [<CLIMutable>] if you want them to have public get set (many libraries have this requirement in C#)
  • when you expose functionality from F# to C#, the very best way to do it (especially if you need dependency injection e.g. in aspnetcore) is to use F# interfaces and classes, so the class in F# will expose to C# all the things.
  • note that many erased type providers don’t work in C# (e.g. FSharp.Data JsonProvider), so you have to “wrap” them in some translation entities, non-erased generated types can be exposed to C# via the F# library instead usually
  • stackoverflow first result: https://stackoverflow.com/questions/478531/call-f-code-from-c-sharp
1 Like