Myths about F#: F# is for FP, C# for OOP! No, F# loves OO.

Sometimes when I talk to C# developers about F#, they say they don’t want to switch to a functional programming language. When I reply that F# is my favourite object-oriented programming language, they look a bit puzzled. They typically think that F# is for functional programming, while C# is for object-oriented programming. And this is wrong!

Both C# and F# support object-oriented and functional programming up to a certain level. One can program in a functional style in C# by defining functions and using lambdas or pattern matching. Or one can use classes and interfaces in F#.

The source of the myth and some history

When C# 1.0 was released, there were no lambdas, no Funcs, and no pattern matching. It was a typical object-oriented language of its time. In C# 3.0, lambda expressions were introduced (see The history of C#). This started C#’s journey towards more and more features typically found in functional-programming languages. So C# has its roots in OOP and added many FP features over the years.

One design goal of F# was to provide great interoperability with C#. So F# had to know about classes and interfaces from its start. F# was a hybrid language from its beginning (see The Early History of F#). However, F# was marketed by Microsoft for many years as a functional-first programming language. They realised that most people understand functional-first as functional and changed their marketing of F# accordingly.

OOP and FP features provided by C# and F#

I tried to summarize the OOP and FP capabilities of C# and F# in the following diagram.

As you can see, F# supports almost all OO features that C# supports – while C# supports (currently) only a small subset of functional programming features. Note that F# supports an object-oriented feature that C# doesn’t have: object expressions.

Also, note that there are OOP and FP features that neither C# nor F# support.

And in case you are a C# developer and wonder what quotations are: they are called expressions in C#.

Feedback

There are a couple of features missing in the diagram above:

  • Partial classes and methods (only supported by C#)
  • Contra/Covariance (only supported in C#)
  • Source Generators (C#), for F# there is Myriad

About the author

Urs Enzler

1 comment

By Urs Enzler

Recent Posts