[英]How do F# types transfer over to C#?
If in F# I have methods like: 如果在F#中我有以下方法:
drawBox
drawSphere
paintImage
Would they be transferred to C#, exactly the same? 它们会被转移到C#,完全一样吗?
If that's the case, then wouldn't it compromise the naming conventions in C#, where methods are supposed to be PascalCase? 如果是这种情况,那么它不会破坏C#中的命名约定,其中方法应该是PascalCase吗?
Or should I make them PascalCase in F# as well to remedy this? 或者我应该在F#中使用PascalCase来解决这个问题吗?
You should follow the F# Component Design Guidelines . 您应该遵循F#组件设计指南 。
The notable parts are: 值得注意的部分是:
invalidArg
), and for the "standard collection functions" (eg List.map
). invalidArg
),用于“标准集合函数”(例如List.map
)。 In both these cases, the function names act much like keywords in the language. Another way to slice it: 切片的另一种方法:
As others already pointed out, the names of compiled members are exactly the same as the names you wrote in the F# code. 正如其他人已经指出的那样,编译成员的名称与您在F#代码中编写的名称完全相同。 In general, it is a good idea to follow standard C# naming conventions when declaring classes.
通常,在声明类时遵循标准C#命名约定是个好主意。 When declaring an F# module then you can either use
camelCase
(especially for modules that are intended for F# users) or PascalCase
if you want to use them from C#. 在声明F#模块时,您可以使用
camelCase
(特别是针对F#用户的模块)或PascalCase
如果您想在C#中使用它们)。
Also, there is one trick that you can use (this is used in the F# core library for functions like List.map
that are actually compiled as List.Map
). 此外,还有一个技巧可以使用(这在F#核心库中用于
List.map
等实际编译为List.Map
)。 You can use the CompiledName
attribute to specify the name in the compiled version: 您可以使用
CompiledName
属性在已编译的版本中指定名称:
module Bar =
[<CompiledName("Foo")>]
let foo a = a + 1
It is probably better to use a unified naming convention, but if you want to keep a nice short name for F# users and standard .NET name for C# users, this is an interesting option. 使用统一的命名约定可能更好,但是如果你想为F#用户保留一个漂亮的短名称,为C#用户保留标准的.NET名称,这是一个有趣的选择。
type methods names will be the same both in F# and C#. 类型方法名称在F#和C#中都是相同的。 if you want PascalCase in C# - you should use this naming convention in F#, the same with camelCase.
如果你想在C#中使用PascalCase - 你应该在F#中使用这个命名约定,与camelCase相同。
Naming conventions are for humans. 命名约定适用于人类。 Compilers and runtimes really don't care if every variable name is entirely unreadable, they'll still work.
编译器和运行时实际上并不关心每个变量名是否完全不可读,它们仍然可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.