F# and C# being bros
All chill and working together and stuff

By Adam Guest - 21 January 2016

Sometimes, you're just coding away in C# trying to be productive and stuff, and then you realise that this would all be easier in F#. But you can't just switch your whole project over to F# (probably because some Project Manager would get mad or something) - but maybe with a bit of interop magic you can get these two .NET languages to be bros and work together nicely. That way, you get to write glorious F# code, but use a bunch of the C# stuff that's already been done.

F# can use all those objects

Like, yeah, in case you were wondering f# can interact with the rest of the .NET world through objects and stuff. Also, since F# 4.0 object constructors are now grown up functions like everything else which means you can use them in pipe chains (I was really excited when I learned this part).

F# can make pretty C# classes

This one is obvious given that it can talk to the rest of the .NET world pretty easily, but yes F# can make classes that C# can understand. Interfaces too! This is how you can make some snazzy F# functions and call them from C# code.

These are full fledged classes and interfaces, so you could also implement an interface defined in F# with a class in C# or vice versa. The big difference is when making static classes - there isn't a proper attribute for that (turns out C# has just been lying to you all this time to make things easier) - but you can still make a static class(ish), just make a sealed class with a private constructor and no instance members:

C# can give F# functions

This is possibly the least elegant part of the whole shebang, but if you have some C# code that you would like to pass to an object created in F# as a Func (which is totally a thing you want to do) you will soon come to notice that a C# Func and an F# function are different things. Fortunately, with some fancy F# plumbing that I found over here you can convert your C# trash into glorious F# functions with a tiny little extension method.

This then means that you can define a class in F# that would take an F# function:

Then you can inject it from your C# code:

Async is also awesome

If you've added async methods to your code, you might've noticed how it spreads itself around like a virus and now everything returns a Task. F# has a different way of working with async compared to C#, but handily it also provides a neat set of functions for moving between Async<'a> and Task<T>.

And so they both lived happily ever after...