top of page
Writer's pictureThe Tech Platform

What is the difference between C# and F#



C#

C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).


C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.


You can use C# to develop different types of secured and robust applications:

  • Window applications

  • Web applications

  • Distributed applications

  • Web service applications

  • Database applications etc.


F#

F# is a functional programming language that supports approaches like object oriented and imperative programming approach. It is a cross-platform and .Net Framework language. It was designed and developed by Microsoft. It was first appeared in 2005. Current stable version of F# is 4.0.1.20 which was released on November 13, 2016.


You can use F# in following domains:

  • Data analysis

  • Scientific research

  • Data statistical

  • Design games

  • Artificial Application

  • Desktop application, and

  • Mobile application etc.



WHAT IS BETTER IN C#


1. TASK RUNTIME PERFORMANCE

Asynchronous code (Tasks) in C# runs faster than in F# because the compiler supports them natively and generates optimized code. The difference may be reduced, once F# supports tasks natively, too.

IMHO: While this is true, I wonder how relevant the difference is for a typical line of business application. For really performance-sensitive code, I’d extract that into a C# library (or something even speedier) and call it from F#.

We use mainly async-workflow in our code because they are easier to use for us than tasks because they are cold, not hot and support cancellation out-of-the-box.


2. INTERACTING WITH .NET LIBRARIES

Most .Net libraries are written in C# and therefore they sometimes are easier to use from C# than F#.


3. EARLY RETURNS

In C# a method can return by just calling return. In F# that is not possible.

This is helpful in deeply nested logic blocks.

IMHO: Early returns are a normal thing in C#, however, they are actually bad design because they make understanding code often very hard.


4. CO/CONTRAVARIANCE

C# supports co/contravariance, F# does not (yet?). This is especially helpful for library code that deals with generics.


5. PROTECTED, PARTIAL CLASSES, INNER CLASS TYPES, IMPLICIT INTERFACE IMPLEMENTATIONS, OOP IN GENERAL

C# has the protected keyword, F# does not. C# knows partial classes, inner class types, and implicit interface implementations, F# does not.

Generally, programming in an object-oriented way goes easier in C#.


6. IMPLICIT CASTING

C# supports implicit casts, F# does not. Neither up, nor downcasts. C# makes it therefore easier to use libraries that rely on implicit casts a lot.

IMHO: No implicit casts reduces defects.


7. SOURCE GENERATORS

Source Generators are unknown to F#. However, there is Myriad.


8. FILE ORDERING

In C#, files and namespaces can be ordered in any way. F# has a strict order (bottom knows top).

Therefore it is a bit easier to define models in C# that need cycles. In F#, you have to use the and keyword and put the types/functions side-by-side.


9. TOOLING AND IDE SUPPORT

C# has the better tooling and IDE support than F#.


10. DEBUGGING

The debugging experience is better in C# across all IDEs than in F#. Especially async workflows are sometimes hard to debug.

This was the most mentioned thing that is better in C# than F#.


11. LOW-LEVEL PROGRAMMING

Things like P/Invoke and unsafe code belong into C#. C# also supports pinned ref and Span.


12. WINFORMS, WPF

C# is made for programming WinForms or WPF clients. That is definitely not the main area of F#.


13. FINDING EXAMPLES AND RESOURCES

It is easier to find examples, solutions and resources for C# in the internet.

IMHO: Of course, there is much more C# content available in the internet. However, when it comes to quality content, I’m not so sure anymore whether F# really lacks behind C#. The F# community is also very helpful – as you can see by the number of responses I got on these tweets.


14. ENTITY FRAMEWORK

Entity Framework is a very popular framework in the .Net world. General consensus is that you should not try to use it from F#. Its design goes directly against F# opinions on immutability and functions-first, OO-second.

IMHO: I would never use EF in C# either. Fully-fledged ORMs like EF have some major drawbacks that I wouldn’t want in my code. I once did a talk on this topic, but there is no video. So the next best thing is to read this.


15. ASYNC MAIN METHOD

In C# you can put async on the main method. In F#, you have to use Async.RunSynchronously in the main method instead.


WHAT IS BETTER IN F#?

These were the things that were mentions that make F# superior:


1. IMMUTABILITY BY DEFAULT

In F#, unless you explicitly use the mutable keyword, everything is immutable. Immutability helps prevent defects and makes parallelization easier.


2. |> PIPES

Pipes in F# allow to write code top-left to bottom-right without using local variables. This makes code easier to read.


3. EVERYTHING IS AN EXPRESSION

Expressions make it easier to reason about code and bugs have a more difficult time to hide. In C#, there are expressions and statements, in F# only expressions.


4. TYPE INFERENCE

F# type inference makes you type less type annotations and makes refactoring easier.


5. DISCRIMINATED UNIONS

F# knows discriminated unions and therefore allows to model a business domain much better than when only using classes, interfaces, enums, and records. Better modelling leads to less defects and simpler code.


6. STRICT ORDER OF FILES

F# has a strict order on the files and the files’ content. Only what is declared above can be used. This prevents cycles by design. Cycles are one of the hardest problems in software design because they make refactoring very hard. You need no additional tool to prevent against such cycles because F# forces you to define no cycles (except for type and function definitions using and).


7. COMPUTATION EXPRESSIONS

F# knows computation expressions, which allow combining different aspects (async, Option, Result, Validation) in a programmer-friendly way. FsToolkit is a great library providing many CEs. In C#, combining async/await with anything results in hard to read and/or bloated code.


PATTERN MATCHING

C# has lately improved a lot regarding pattern matching. However, F# has still an advantage. F# also knows Active Pattern that can make complicated matching patterns easy to read.


1. MEASURES

F# know measures, C# does not. Measures make code more expressive and less bug-prone in calculations.


2. EASIER TO MERGE

In C#, many merge conflicts are caused be , and ) at the end of a method declaration. F# does not have this problem.


3. F# IS THE SIMPLER LANGUAGE

F# is built for simplicity. F# currently knows 71 keywords, C# has above 110.

F# has only one way to define a record, C# has 4.



C# Example:


Sum of Square

public static class SumOfSquareHelper
{
    public static int square (int i)
    {
        return i * j;
    }
    
    public static int SumOfSquares(int n)
    {
        int sum=0;
        for (int i = 1; i <= n; i++)
        {
            sum += Square(i);
        }
        return sum;
    }
}

F# Example


Sum of Square

Let square x = x * x

Let SumOfSquare n =
    [1..n] | > List.map sqauer | > List.sum
    
SumOfSquares 100


Resource: planetgeek, WIkipedia,


The Tech Platform

0 comments

Comentarios


bottom of page