Key Differences between C# and F#

14 juni 2021 om 10:00 by ParTech Media - Post a comment

Both C# and F# are unique in their own ways. And obviously, there are certain similarities and differences between these two languages which are bound to exist when we compare different programming languages.

So what are these differences? What are the things that you can do better in C# and F#? We have answers to all your questions in this post. Read on.

Table of contents

  • Where C# scores over F#?
  • Where F# scores over C#?
  • Comparison of codes in both languages
  • Conclusion

Where C# scores over F#?

Task Runtime Performance

Asynchronous code runs faster in C# than in F#. This is primarily because these codes are natively supported by the compiler and they generate optimized code. Once F# starts supporting codes natively, this difference will be reduced. Though this difference exists, it is not very critical when it comes to a typical line of business application.

W can rather extract into a C# library and call it from F#, for a real performance-sensitive code. Async-workflow is generally used in the code as it is easier to use than a task. Also because it supports cancellation out of the box.

Interacting with .NET libraries

Since most of the .NET libraries are written in C#, it becomes easier for the developer to work in C# as compared to F#.

Early Returns

A method can be called in C# by just using the keyword return. This is not possible in F#. In deeply nested code blocks, this feature is really useful. Though return makes calling a method easier, understanding the code might get difficult.

Co/Contravariance

Co/Contravariance is supported in C#, which is not supported in F# as of now. This is particularly beneficial for the library codes that deal with generics.

Object-Oriented Programming in general

Working with protected classes is easier in C# as it has a keyword called protected which is absent in F#. Also, the implementation of inner class types, implicit interfaces, and partial classes is possible in C# and not in F#. General object-oriented programming is done more easily in C#.

Implicit Casting

Implicit casting is supported in C# while it is not supported in F#. Neith of the two i.e. upcast or downcast is supported in F#. Hence, it is easier to use libraries relying on implicit casting in C#.

Source Generators

Source generators are not available for F#. However, there is Myriad (a code generator).

File ordering

In C#, ordering the files and namespaces is possible in any way. There is a strict order in F# (bottom to top). Hence, defining the models that need cycles is easier in C#.

Tooling and Integrated Development Environment Support

Tooling and better IDE support are there in C# as compared to F#.

Debugging

In all IDEs, the debugging process is easier in C# than in F#. Async workflows are particularly difficult to debug in F#.

Low-level programming

Unsafe code and Invoke/P are the things that belong to C#. Span and pinned ref are also supported in C#. These are absent in F#.

Winforms or WPF

C# was particularly started to code for WPF or Winform clients. This is not an important area for F#.

Entity Framework

In the .NET world, the Entity Framework is a very popular framework. It comes instinctively to the coders to not use this framework in F# because the design of this framework is against F#.

Async main method

In the main methodm async can be used in the case of C# while for F# Async.RunSynchronously is used in the main method.

Where F# scores over C#?

Immutability by default

Everything is immutable in F# unless you use the keyword mutable. Immutability helps in making parallelization easier and preventing defects.

Pipes

In F#, Pipes facilitates coding from top-left to bottom-right without the usage of any local variables. Hence making it easier to read the code.

Everything is an expression

Reasoning about the code becomes easier with the usage of expressions and debugging the code becomes easier as well. There are only expressions in F# while there are expressions and statements in C#.

Type inference

Type inference in F# makes the programmer type lesser annotations. Refactoring is made easier as well.

Discriminated Unions

Discriminated Unions are present in F# that allows modeling a business domain in a better way when using classes, records, enums, and interfaces. Better modeling results in simple code and fewer defects.

Computation Expressions

Combining various aspects such as async, Option, Result, Validation, etc, in a programmer-friendly manner is Computation Expression. This is well known in F#. Many CEs are provided in a library named FsToolkit. Combining async with anything ends up into a bloated code and is difficult to read.

Pattern Matching

Active patterns in F# make complicated pattern matching easier to read. Though C# has lately improved in pattern matching, F# has an upper edge with an Active pattern over C#.

Measures

Measures are known in F# while not in C#. The code becomes less bug-prone and more expressive with the usage of measures.

Easier to Merge

Many merge conflicts are caused in C# at the end of a method declaration. No such problem arises in F#.

F# is the simpler language

Simplicity is the main reason for using F#. There are 71 keywords in F# whereas there are 110 in C#. There is only one way to define a record in F# while there are 4 in C#.

Fantomas

There is no auto syntax layout tool for C# though tooling. F# has Fantomas that helps in writing readable code without any StyleCop-build-warnings.

Partial application and Composability

Partial application is provided in F# which is not facilitated in C#. This helps in creating a better composable design.

Comparison of codes in both languages

C#: Sum of squares

F#: Sum of Squares

Conclusion

In this article, we have seen how C# is better than F# in certain aspects and vice versa. Both languages have their own advantages and disadvantages. Hence, it is important to select the language based on the application you are building, so that full advantage of their features can be utilized to write a good code. And the best part is- the interoperability is amazing between these two languages letting you mix them and use them in your application.