As a C++11 programmer you’ve probably played with type inference and lambda functions and may now feel curious about trying a functional first programming language like F#. F# is a first class .Net programming language that ships with Visual Studio. Although C# shares the familiar moustache based syntax of C++, you may like me actually find F# more familiar in terms of power features like immutability, function inlining and even sprintf .
Comparing some of the good parts of C++11 with C# and F#:
| C++11 | C# | F# |
Architect
| Committee formerly B. Stroustrup | Anders Hejlsberg* | Don Syme |
Statically typed | Yes | Yes
| Yes
|
Generic programming | Template classes, methods & functions e.g. list<T> | Generic types & methods e.g. List<T> | Generic types, methods & functions e.g. list<T> |
Type aliasing | typedef keyword e.g. typedef list<int> xs; | using directive e.g. using xs=List<T>; | type keyword e.g. type xs = list<int> |
Type inference | auto keyword on local & global variables | var keyword on local variables | let keyword on values, variables, parameters & return values |
Math functions | Built-in e.g. abs, min, sin, pow | System.Math class e.g. Math.Abs | Built-in e.g. abs, min, sin, pown |
String format | sprintf function e.g. sprintf(“%d”, 1) | String.Format method String.Format(“{0}”,1) | sprintf function e.g. sprintf “%d” 1 |
Inline functions | inline keyword
| N/A | inline keyword |
Immutability | const correctness mutable keyword | readonly fields | Immutable by default mutable keyword |
Immutable collections | N/A | N/A | Built-in e.g. list, set, map |
Agents | Concurrency runtime Agents library | DevLabs project TPL Dataflow | Built-in MailboxProcessor<T> |
Resource management | Resource acquisition is initialization (RAII) | Explicit scope with using statement on IDisposable objects | Automatic scope with use keyword or explicit scope with using function |
Interop | COM & P/Invoke | P/Invoke & COM | P/Invoke & COM |
* Note: Anders Hejlsberg is reported to be currently working on JavaScript tooling
Resource management
In .Net, garbage collection replaces C++’s delete method for reclaiming memory. For resources like files or database connections that implement IDisposable, .Net can feel a bit more clumsy requiring explicit disposal, over using C++ with RAII.
In C# to determine if a class implements IDisposable you must look at the implementation, and then explicitly dispose the object with either a using block or calling the Dispose method.
using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
In F# you can tell if a class implements IDisposable by hovering over it in Visual Studio. Also types implementing IDisposable must be created using the new keyword, which gives an explicit code indication. Finally F# has a use keyword which can be used in place of the let keyword for IDisposable types and automatically calls Dispose when the object is out of scope.
use font1 = new Font("Arial", 10.0f)
let charset = font1.GdiCharSet
Print
Like C++, F# provides a printf and sprintf function. What may be a little more surprising is that in F# they are type safe:

F# will generate a compile time error if the number or type of parameters don’t match.
Headers
Although not required F# supports signatures which are akin to C++ header files and useful for libraries (for example F#’s built-in libraries uses them), particularly for separating code documentation examples from the actual implementation code.
Summary
If you’re interested in augmenting your high performance C++ code with some high level functional code, look beyond the syntax and you may find F# more familiar and feature rich.