How to use a F# library in a C# application

Within this article I want to show you the steps you have to do if you want to use a F# library in a C# application.

 
Step 1: Create the F# library
At first you have to create a new project of type F# library. In the following example an math library with two functions will be shown.

module MyMathLib

let addition x y = x + y

let multiplication x y = x*y

 
Step 2: Use the F# library in a C# application
Now you have to create your C# application, for example a console application. Within you application you may use your F# library by calling the created functions. The following source code shows a console application example.

using System;

namespace UseFSharpInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MyMathLib.addition(1, 2));
            Console.WriteLine(MyMathLib.multiplication(3, 2));
            Console.ReadKey();
        }
    }
}

Depending on the F# features you want to use, you sometimes have to add a reference to the F# core library. For example if you want to use a function as parameter for a F# function, you have to add a reference to Microsoft.FSharp.Core to your C# project references and you must include the using command for Microsoft.FSharp.Core within you source code. Now you may use the F# features within your C# application.

 
Guidelines for user friendly F# libraries
F# has a rich and powerful type system. But these types may look a little bit unusual in other .NET languages. Although you can call any F# library from any other .NET language. But if you use a lot of the F# data types the library is not very user friendly. The following list contains guidelines to create a user friendly library:

  • Hide implementation details of you library by using the private or internal keyword for modules, functions, variables and all other elements of your library which should not be public.
  • Avoid functions that return tuples.
  • If you have a public function which takes another function as parameter then expose this parameter as a delegate.
  • Do not use union types.
  • If union types are really needed, then add members to make them easier to use.
  • Avoid functions that return lists. Use arrays, collections ore enumerable objects instead.

 
Summary
It is very easy to use an F# library within a C# application. But to create a user friendly library you should follow the rules which were shown above.

Advertisements
Dieser Beitrag wurde unter .NET, F# abgelegt und mit , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s