Using F# with existing C# libraries - Emmanuel Katto Uganda

Hello everyone, My name is Emmanuel Katto. I wanted to know how can I effectively use F# with existing C# libraries? Are there any common pitfalls to watch out for?

Looking forward to responses.

Thanks in advance,
Emmanuel Katto

Welcome to the forums Emmanuel.

You can refer to, and use, the contents of pretty-much any up-to-date C# library from an F# project, as long as the versions are compatible.

There may be some exceptions but I’ve not used them all (or even most of them).

Probably the best advice in this area that I can give is to make sure that you check for null values coming in from C# code and deal with them as soon as possible (maybe by using an Option).

Also, some constructs, for example Discriminated Unions, are not available in C# so you may need to use an ‘interop’ class/record to move data between the languages at times.

I’ve read that dynamic typing isn’t well-supported in F# but I’ve not used that much myself, and I can’t say if that’s still true with the latest version of F#. (I’ve also read that it’s supposed to be quite easy to get round those limitations so that might not be a big deal.)

Since the number of C# libraries is very large, and very few people will have used them all, if you can give us an idea of which libraries you might want to use then someone might be able to give you an answer which is more specific to your needs.

The entire .Net BCL (base class library) is C# libraries. As GarryP said, just watch out for null references being returned. F# 9 coming soon will be better here as it supports Nullable Reference Types which were introduced with C# in some recent version and which many libraries have been updated with the required annotations for it.