使用 C# 与 F# 或 F# 与 C# 有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/952318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:06:39  来源:igfitidea点击:

What are the benefits of using C# vs F# or F# vs C#?

c#.netf#

提问by gradbot

I work for a tech company that does more prototyping than product shipment. I just got asked what's the difference between C# and F#, why did MS create F# and what scenarios would it be better than C#.

我在一家技术公司工作,该公司的原型设计比产品运输多。我刚刚被问到 C# 和 F# 之间有什么区别,为什么 MS 创建 F# 以及它比 C# 更好的场景。

I've been using the language for a while now and I love it so I could easily go on about the great features of F# however I lack the experience in C# to say why we should use one over the other.

我已经使用该语言一段时间了,我喜欢它,所以我可以轻松地继续介绍 F# 的强大功能,但是我缺乏 C# 方面的经验,无法说明为什么我们应该使用一种而不是另一种。

What's the benefits of using C# vs F# or F# vs C#?

使用 C# 与 F# 或 F# 与 C# 的好处是什么?

采纳答案by Dario

General benefits of functional programming over imperative languages:

函数式编程相对于命令式语言的一般优势:

You can formulate many problems much easier, closer to their definition and more concise in a functional programming language like F# and your code is less error-prone (immutability, more powerful type system, intuitive recurive algorithms). You can code what you mean instead of what the computer wants you to say ;-) You will find many discussions like this when you google it or even search for it at SO.

在函数式编程语言(如 F#)中,您可以更轻松、更接近其定义、更简洁地表述许多问题,并且您的代码不易出错(不变性、更强大的类型系统、直观的递归算法)。你可以编码你的意思,而不是计算机想让你说的;-)当你谷歌它甚至在 SO 搜索它时,你会发现很多这样的讨论。

Special F#-advantages:

F# 的特殊优势:

  • Asynchronous programming is extremelyeasy and intuitive with async {}-expressions- Even with ParallelFX, the corresponding C#-code is much bigger

  • Very easy integrationof compiler compilers and domain-specific languages

  • Extending the language as you need it: LOP

  • Units of measure

  • More flexible syntax

  • Often shorter and more elegant solutions

Take a look at this document

看看这个文件

The advantages of C# are that it's often more accurate to "imperative"-applications (User-interface, imperative algorithms) than a functional programming language, that the .NET-Framework it uses is designed imperatively and that it's more widespread.

C# 的优点是它通常比函数式编程语言更准确地用于“命令式”应用程序(用户界面、命令式算法),它使用的 .NET 框架是命令式设计的,并且更广泛。

Furthermore you can have F# and C# together in one solution, so you can combine the benefits of both languages and use them where they're needed.

此外,您可以在一个解决方案中同时使用 F# 和 C#,这样您就可以结合两种语言的优点并在需要的地方使用它们。

回答by Rinat Abdullin

  • F# Has Better Performance than C# in Math
  • You could use F# projects in the same solution with C# (and call from one to another)
  • F# is really good for complex algorithmic programming, financial and scientific applications
  • F# logically is really good for the parallel execution (it is easier to make F# code execute on parallel cores, than C#)
  • F# 在数学方面的表现比 C# 好
  • 您可以在与 C# 相同的解决方案中使用 F# 项目(并从一个调用到另一个)
  • F# 非常适合复杂的算法编程、金融和科学应用
  • F# 在逻辑上非常适合并行执行(在并行内核上执行 F# 代码比 C# 更容易)

回答by Spencer Ruport

You're asking for a comparison between a procedural language and a functional language so I feel your question can be answered here: What is the difference between procedural programming and functional programming?

您要求对过程语言和函数式语言进行比较,所以我觉得可以在这里回答您的问题:过程编程和函数式编程之间有什么区别?

As to why MS created F# the answer is simply: Creating a functional language with access to the .Net library simply expanded their market base. And seeing how the syntax is nearly identical to OCaml, it really didn't require much effort on their part.

至于为什么 MS 创建 F#,答案很简单:创建一种可以访问 .Net 库的函数式语言只是扩大了他们的市场基础。看到语法与 OCaml 几乎相同,他们真的不需要太多努力。

回答by Rex M

It's like asking what's the benefit of a hammer over a screwdriver. At an extremely high level, both do essentially the same thing, but at the implementation level it's important to select the optimal tool for what you're trying to accomplish. There are tasks that are difficult and time-consuming in c# but easy in f# - like trying to pound a nail with a screwdriver. You can do it, for sure - it's just not ideal.

这就像问与螺丝刀相比,锤子有什么好处。在极高的层次上,两者本质上做相同的事情,但在实现层次上,重要的是为您要完成的任务选择最佳工具。有些任务在 c# 中既困难又耗时,但在 f# 中却很容易——比如尝试用螺丝刀敲钉子。你可以做到,当然 - 这并不理想。

Data manipulation is one example I can personally point to where f# really shines and c# can potentially be unwieldy. On the flip side, I'd say (generally speaking) complex stateful UI is easier in OO (c#) than functional (f#). (There would probably be some people who disagree with this since it's "cool" right now to "prove" how easy it is to do anythingin F#, but I stand by it). There are countless others.

数据操作是我个人可以指出 f# 真正发挥作用的一个例子,而 c# 可能很笨拙。另一方面,我会说(一般来说)复杂的有状态 UI 在面向对象 (c#) 中比在功能 (f#) 中更容易。(可能会有一些人不同意这一点,因为现在“证明”在 F# 中做任何事情是多么容易“很酷” ,但我支持它)。还有无数其他的。

回答by Jeroen Huinink

F# is not yet-another-programming-language if you are comparing it to C#, C++, VB. C#, C, VB are all imperative or procedural programming languages. F# is a functional programming language.

如果您将 F# 与 C#、C++ 和 VB 进行比较,F# 还不是另一种编程语言。C#、C、VB 都是命令式或过程式编程语言。F# 是一种函数式编程语言。

Two main benefits of functional programming languages (compared to imperative languages) are 1. that they don't have side-effects. This makes mathematical reasoning about properties of your program a lot easier. 2. that functions are first class citizens. You can pass functions as parameters to another functions just as easily as you can other values.

函数式编程语言(与命令式语言相比)的两个主要优点是 1. 它们没有副作用。这使得关于程序属性的数学推理变得更加容易。2. 函数是一等公民。您可以像传递其他值一样轻松地将函数作为参数传递给另一个函数。

Both imperative and functional programming languages have their uses. Although I have not done any serious work in F# yet, we are currently implementing a scheduling component in one of our products based on C# and are going to do an experiment by coding the same scheduler in F# as well to see if the correctness of the implementation can be validated more easily than with the C# equivalent.

命令式和函数式编程语言都有其用途。虽然我还没有在 F# 中做过任何认真的工作,但我们目前正在我们的一个基于 C# 的产品中实现一个调度组件,并将通过在 F# 中编写相同的调度程序来做一个实验,看看是否正确与 C# 等效项相比,可以更轻松地验证实现。

回答by Jonathan Allen

F# is essentially the C++ of functional programming languages. They kept almost everything from Objective Caml, including the really stupid parts, and threw it on top of the .NET runtime in such a way that it brings in all the bad things from .NET as well.

F# 本质上是函数式编程语言的 C++。他们保留了 Objective Caml 中的几乎所有内容,包括非常愚蠢的部分,并将其放在 .NET 运行时之上,这样它也引入了 .NET 中的所有不好的东西。

For example, with Objective Caml you get one type of null, the option<T>. With F# you get three types of null, option<T>, Nullable<T>, and reference nulls. This means if you have an option you need to first check to see if it is "None", then you need to check if it is "Some(null)".

例如,使用 Objective Caml,您会得到一种类型的 null,即 option<T>。使用 F#,您可以获得三种类型的空值、option<T>、Nullable<T> 和引用空值。这意味着如果您有一个选项,您需要先检查它是否为“None”,然后您需要检查它是否为“Some(null)”。

F# is like the old Java clone J#, just a bastardized language just to attract attention. Some people will love it, a few of those will even use it, but in the end it is still a 20-year-old language tacked onto the CLR.

F# 就像旧的 Java 克隆 J#,只是一种为了引起注意的混蛋语言。有些人会喜欢它,其中一些人甚至会使用它,但最终它仍然是一种 20 岁的语言,并附加到 CLR 上。

回答by MichaelGG

To answer your question as I understand it: Why use C#? (You say you're already sold on F#.)

按照我的理解回答你的问题:为什么使用 C#?(你说你已经在 F# 上出售了。)

First off. It's not just "functional versus OO". It's "Functional+OO versus OO". C#'s functional features are pretty rudimentary. F#'s are not. Meanwhile, F# does almost all of C#'s OO features. For the most part, F# ends up as a superset of C#'s functionality.

首先。这不仅仅是“功能与 OO”。这是“功能 + OO 与 OO”。C# 的功能特性非常简陋。F# 不是。同时,F# 几乎完成了 C# 的所有 OO 功能。在大多数情况下,F# 最终成为 C# 功能的超集。

However, there are a few cases where F# might not be the best choice:

但是,在某些情况下,F# 可能不是最佳选择:

  • Interop. There are plenty of libraries that just aren't going to be too comfortable from F#. Maybe they exploit certain C# OO things that F# doesn't do the same, or perhaps they rely on internals of the C# compiler. For example, Expression. While you can easily turn an F# quotation into an Expression, the result is not always exactly what C# would create. Certain libraries have a problem with this.

    • Yes, interop is a pretty big net and can result in a bit of friction with some libraries.

    • I consider interop to also include if you have a large existing codebase. It might not make sense to just start writing parts in F#.

  • Design tools. F# doesn't have any. Does not mean it couldn'thave any, but just right now you can't whip up a WinForms app with F# codebehind. Even where it is supported, like in ASPX pages, you don't currently get IntelliSense. So, you need to carefully consider where your boundaries will be for generated code. On a really tiny project that almost exclusively uses the various designers, it might not be worth it to use F# for the "glue" or logic. On larger projects, this might become less of an issue.

    • This isn't an intrinsic problem. Unlike the Rex M's answer, I don't see anything intrinsic about C# or F# that make them better to do a UI with lots of mutable fields. Maybe he was referring to the extra overhead of having to write "mutable" and using <- instead of =.

    • Also depends on the library/designer used. We love using ASP.NET MVC with F# for all the controllers, then a C# web project to get the ASPX designers. We mix the actual ASPX "code inline" between C# and F#, depending on what we need on that page. (IntelliSense versus F# types.)

  • Other tools. They might just be expecting C# only and not know how to deal with F# projects or compiled code. Also, F#'s libraries don't ship as part of .NET, so you have a bit extra to ship around.

  • But the number one issue? People. If none of your developers want to learn F#, or worse, have severe difficulty comprehending certain aspects, then you're probably toast. (Although, I'd argue you're toast anyways in that case.) Oh, and if management says no, that might be an issue.

  • 互操作。有很多库不会让 F# 太舒服。也许他们利用了某些 C# OO 的东西,而 F# 没有这样做,或者他们可能依赖 C# 编译器的内部结构。例如,表达式。虽然您可以轻松地将 F# 引用转换为表达式,但结果并不总是与 C# 完全相同。某些图书馆对此有问题。

    • 是的,互操作是一个相当大的网络,可能会导致与某些库的一些摩擦。

    • 如果您有一个大型的现有代码库,我认为互操作也包括在内。开始在 F# 中编写部分可能没有意义。

  • 设计工具。F# 没有。并不意味着它不能有任何,但就目前而言,您无法使用 F# 代码隐藏来创建 WinForms 应用程序。即使在支持它的地方(例如在 ASPX 页面中),您目前也无法获得 IntelliSense。因此,您需要仔细考虑生成代码的边界。在几乎完全使用各种设计器的非常小的项目中,将 F# 用于“胶水”或逻辑可能不值得。在较大的项目中,这可能不再是一个问题。

    • 这不是一个内在的问题。与 Rex M 的回答不同,我没有看到 C# 或 F# 的任何内在特性使它们更好地制作具有大量可变字段的 UI。也许他指的是必须编写“可变”并使用 <- 而不是 = 的额外开销。

    • 还取决于使用的库/设计器。我们喜欢将 ASP.NET MVC 与 F# 用于所有控制器,然后使用 C# Web 项目来获取 ASPX 设计器。我们在 C# 和 F# 之间混合了实际的 ASPX“内联代码”,这取决于我们在该页面上需要什么。(IntelliSense 与 F# 类型。)

  • 其他工具。他们可能只是期待 C#,而不知道如何处理 F# 项目或编译代码。此外,F# 的库不作为 .NET 的一部分提供,因此您还有一些额外的东西需要提供。

  • 但第一个问题呢?人们。如果您的开发人员都不想学习 F#,或者更糟的是,在理解某些方面有严重困难,那么您可能会干杯。(虽然,我认为在这种情况下无论如何你都在干杯。)哦,如果管理层拒绝,那可能是一个问题。

I wrote about this a while ago: Why NOT F#?

我不久前写过这个:为什么不是 F#?

回答by Joh

One of the aspects of .NET I like the most are generics. Even if you write procedural code in F#, you will still benefit from type inference. It makes writing generic code easy.

我最喜欢的 .NET 方面之一是泛型。即使您在 F# 中编写过程代码,您仍然会受益于类型推断。它使编写通用代码变得容易。

In C#, you write concrete code by default, and you have to put in some extra work to write generic code.

在 C# 中,默认情况下您编写具体代码,并且您必须付出一些额外的工作来编写通用代码。

In F#, you write generic code by default. After spending over a year of programming in both F# and C#, I find that library code I write in F# is both more concise and more generic than the code I write in C#, and is therefore also more reusable. I miss many opportunities to write generic code in C#, probably because I'm blinded by the mandatory type annotations.

在 F# 中,默认情况下您编写通用代码。经过一年多的 F# 和 C# 编程,我发现我用 F# 编写的库代码比我用 C# 编写的代码更简洁、更通用,因此也更可重用。我错过了很多用 C# 编写泛型代码的机会,可能是因为我被强制类型注释蒙蔽了双眼。

There are however situations where using C# is preferable, depending on one's taste and programming style.

然而,在某些情况下,使用 C# 更可取,这取决于个人的品味和编程风格。

  • C# does not impose an order of declaration among types, and it's not sensitive to the order in which files are compiled.
  • C# has some implicit conversions that F# cannot afford because of type inference.
  • C# 不强加类型之间的声明顺序,并且它对编译文件的顺序不敏感。
  • 由于类型推断,C# 有一些 F# 无法承受的隐式转换。