Lesson 25. Consuming C# from F#
In the real world, you’ll be hard-pressed to write a purely F# application. Not only are most of the NuGet packages out there written in C#, but the entire BCL is too. You’ve been working with C# types and objects from F# and so you know how to deal with the basics already, but a few extra areas are worth touching upon in order to round off this subject. We’ll look at these topics:
- Creating hybrid solutions
- Using Visual Studio tools
- Consuming assemblies from scripts
- Consuming classes and interfaces in F#
- Safely working with nullable objects in F#
In addition to working with the BCL code base, virtually any C# code you write today can be consumed from F#. This includes the following:
- All BCL code
- .NET assemblies including NuGet packages (see lesson 26)
- Sibling projects in the same solution
In other words, you can accomplish the same tasks in F# that you perform today in C#. With that bold statement in mind, let’s see how to create a solution that uses both C# and F# code right now!
Dynamic F#
One area with richer support in C#, as compared to F#, is dynamic typing. F# does have some support for some dynamic typing through the custom ? operator, which allows you to handle dynamic member access in a similar vein to C#’s dynamic object types, but this feature is rarely used. F# also has meta-programming features such as type providers, which you’ll see in the next unit, that somewhat alleviate the need for dynamic typing.