简体   繁体   English

F# c# 中的中缀运算符?

[英]F# infix operators in c#?

In F# it is not uncommon to declare infix operators for binary operators.在 F# 中,为二元运算符声明中缀运算符并不少见。 But how are they represented if we try to use them in C# Since there are no way of declaring infix operators in C#?但是如果我们尝试在 C# 中使用它们,它们是如何表示的,因为在 C# 中无法声明中缀运算符?

Toy Example玩具示例

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

If you check the IL representation of your operator, you will see this:如果您检查操作员的 IL 表示,您将看到:

 .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# .不幸的是,您将无法从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 :然后你可以从 C# 中引用你的 F# dll 并通过addop调用它:

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

3 3

Process finished with exit code 0.进程以退出代码 0 结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM