F# と C# の記法比較

MSDN F# リファレンスを使い C# と記法を比較する

外部関数

外部関数 External Functions

元ネタ http://msdn.microsoft.com/ja-jp/library/dd393785.aspx

次のエクスポート関数を含むネイティブ C++ DLL があると仮定します。

#include <stdio.h>
extern "C" void __declspec(dllexport) HelloWorld()
{
    printf("Hello world, invoked by F#!\n");
}

この関数は、次のコードを使用して F# から呼び出すことができます。

F#

open System.Runtime.InteropServices

module InteropWithNative =
    [<DllImport(@"C:\bin\nativedll", CallingConvention = CallingConvention.Cdecl)>]
    extern void HelloWorld()

InteropWithNative.HelloWorld()

書き方は C# でも F# でも同じ。

C#

using System.Runtime.InteropServices;

class InteropWithNative
{
    [DllImport(@"C:\bin\nativedll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void HelloWorld();
}

class Program()
{
    void Main()
    {
        InteropWithNative.HelloWorld();
    }
}