Simulating Algebraic Data Types (AKA tagged unions) and pattern matching them in C#

After programming for a bit in Scala I’ve fallen in love with functional programming. And given how concise the code is there’s not much to hate in it.

One of the fine features of Scala is an easy way of defining ADT’s.

Here’s a comparision of defining same ADT with C# and Scala:

Scala is a lot more concise, but that’s just the way C# is – noisy.

Now lets take a look on traditional approach of matching those.



Good thing we can at least use some custom code and functional magic to make that similar to our C# version.

Given that we use this:

We can transform C# code into following:

Which isn’t perfect but is a whole lot nicer.