When should I use C# vs F#?

Hey! Many years ago I used C# professionally before F# was a commonly used language. Now it seems that F# has taken off too, and that much of the functionality (and language-style) is common between the two. If I was reaching for a .net language in 2022, how should I choose one over the other - are there specific use-cases that make one a better choice? Or some language features that really separate them? Thanks!

One of the common misconceptions is that F# is only useful for specific domains, like math or finance or the like. F# actually is a general-purpose programming language, and can basically do anything C# does but better (IMHO). Some benefits of using F# over C#:

  • Correctness: due to a more advanced compiler and some language features (e.g. no null), F# code usually has less bug
  • Conciseness: you usually need less lines of code
  • Expressiveness: F# is great to model your application’s domain

I’d also argue that F# is a better designed language than C# in that it has less syntax, less ways of doing the same thing and just seems to be more “fun” to write code in.

The main arguments against using F# are that functional programming is unfamiliar to many and that it is hard to find people that know F#. Personally, I think both arguments can be circumvented by having one or two people in your team that know F# help the others learn F#. As F# also supports object-oriented programming, transitioning from C# to F# can be a nice and gradual process.

Scott Wlaschin explains it much better than I can on Why use F#? | F# for fun and profit and Anything C# can do... | F# for fun and profit

3 Likes

One of the issues can be finding examples/tooling for newer dotnet features. For instance, last I checked, MAUI was not particularly F# friendly.

2 Likes

That’s true. Newer Microsoft products usually only really get C# support. It is often up to the community to build an F#-friendly version on top of those products. A good example of this is Giraffe, which is a web framework built on top of ASP.NET.

2 Likes