Skip to content
This repository has been archived by the owner on Nov 20, 2021. It is now read-only.

TolikPylypchuk/PatternMatching

Repository files navigation

PatternMatching

NuGet

A library which enables more powerful pattern matching than is currently available in the C#'s switch statement/expression.

New version

This repository is deprecated. Version 2+ can be found here: https://github.com/TolikPylypchuk/Matchmaker

Installation

Install-Package CSharpStuff.PatternMatching -Version 1.2.0

A simple example

This is what the simplest match expression looks like:

using static PatternMatching.Pattern;

// ...

string result =
    Match.Create<int, string>()
        .Case(EqualTo(1), _ => "one")
        .Case(EqualTo(2), _ => "two")
        .Case(EqualTo(3), _ => "three")
        .Case(EqualTo(4), _ => "four")
        .Case(Any<int>(), i => i.ToString())
        .ExecuteOn(5);

EqualTo is a predefined pattern.

This is what an equivalent switch statement looks like:

string result;
int i = 5;

switch (i)
{
    case 1:
        result = "one";
        break;
    case 2:
        result = "two";
        break;
    case 3:
        result = "three";
        break;
    case 4:
        result = "four";
        break;
    default:
        result = i.ToString();
        break;
}

While this example doesn't show the full power of pattern matching, there are a few things to note here:

  • The match expression yields a result. We don't have to assign the result explicitly in each case.

  • The input of the match expression is specified after all the cases. This allows us to save the match expression in an object, and use it multiple times on different input values.

  • The default case is a pattern, just like any other. It's called Any and is always matched successfully.

  • Like in switch the patterns are tried out sequentially. This means that the Any pattern should always come last.

More Info

The documentation can be found here:

License

MIT License