F# infix operators in c#?

340 views Asked by At

In F# it is not uncommon to declare infix operators for binary operators. But how are they represented if we try to use them in C# Since there are no way of declaring infix operators in C#?

Toy Example

let ( .|. ) a b = a + b
1

There are 1 answers

1
s952163 On

If you check the IL representation of your operator, you will see this:

 .method public static specialname int32
    op_DotBarDot(
      int32 a,
      int32 b
    ) cil managed

Unfortunately you won't be able to refer to this from C#. However you can add an attribute:

[<CompiledName("addop")>]
let ( .|. ) a b = a + b

Then you can just reference your F# dll from C# and call it via addop:

Console.WriteLine(global::Program.addop(1,2));

3

Process finished with exit code 0.