Date:

Share:

C# Tip: List Pattern to match an collection against a sequence of patterns

Related Articles

With C# 11 we have an interesting new feature: A list of patterns.

You can, in fact, use is operator to check if the array has the exact shape you expect.

Take this method as an example.

Introduction to list patterns

string YeahOrError(int[] s)
{
    if (s is [1, 2, 3]) return "YEAH";
    return "error!";
}

As you can imagine, the previous method returns YEAH if the input array is exactly [1, 2, 3]. You can, in fact, test it by running some tests:

[Test]
public void PatternMatchingWorks()
{
    Assert.That(YeahOrError(new int[] { 1, 2, 3 }), Is.EqualTo("YEAH"));
    Assert.That(YeahOrError(new int[] { 1, 2, 3, 4 }), Is.EqualTo("error!"));
    Assert.That(YeahOrError(new int[] { 2, 3, 1}), Is.EqualTo("error!"));
}

As you can see, if the order is different, the check doesn’t go through.

Register templates with Discard

We can also use throw Values ​​to check if a list contains a specific item at a specific location, ignoring all other values:

string YeahOrErrorWithDiscard(int[] s)
{
    if (s is [_, 2, _]) return "YEAH";
    return "error!";
}

Therefore, to be valid, the array must contain exactly 3 elements, and the second must be “2”.

[Test]
public void PatternMatchingWorksWithDiscard()
{
    Assert.That(YeahOrErrorWithDiscard(new int[] { 1, 2, 3 }), Is.EqualTo("YEAH"));
    Assert.That(YeahOrErrorWithDiscard(new int[] { 9, 2, 6 }), Is.EqualTo("YEAH"));
    Assert.That(YeahOrErrorWithDiscard(new int[] { 1, 6, 2, 3 }), Is.EqualTo("error!"));
    Assert.That(YeahOrErrorWithDiscard(new int[] { 6, 3, 8, 4 }), Is.EqualTo("error!"));
}

List of templates with variable assignment

you can too assign one or more such arrays to a variableand discard everything else:

string SelfOrMessageWithVar(int[] s)
{
    if (s is [_, 2, int third]) return "YEAH_" + third;
    return "error!";
}

the previous condition, s is [_, 2, int third]Rehearsals true Only if the array has 3 elements, and the second is “2”. It then stores the third element in a new variable, int thirdand uses it to construct the returned string.

[Test]
public void can_use_list_patterns_with_var()
{
    Assert.That(SelfOrMessageWithVar(new int[] { 1, 2, 3 }), Is.EqualTo("YEAH_3"));
    Assert.That(SelfOrMessageWithVar(new int[] { 1, 6, 2, 3 }), Is.EqualTo("error!"));
    Assert.That(SelfOrMessageWithVar(new int[] { 6, 3, 8, 4 }), Is.EqualTo("error!"));
}

List of templates with item constraints

Finally, you can also specify additional constraints on each value in the condition, using operators such as or, >, >=and all.

string SelfOrMessageWithCondition(int[] s)
{
    if (s is [0 or 1, > 2, int third]) return "YEAH_" + third;
    return "error!";
}

You can easily guess the meaning of the previous method. You can double check the actual result by looking at the following tests:

[Test]
[DotNet7]
public void can_use_list_patterns_with_condition()
{
    Assert.That(SelfOrMessageWithCondition(new int[] { 0, 4, 3 }), Is.EqualTo("YEAH_3"));
    Assert.That(SelfOrMessageWithCondition(new int[] { 6, 4, 3 }), Is.EqualTo("error!"));
    Assert.That(SelfOrMessageWithCondition(new int[] { 1, 2, 3 }), Is.EqualTo("error!"));
    Assert.That(SelfOrMessageWithCondition(new int[] { 1, 6, 2, 3 }), Is.EqualTo("error!"));
    Assert.That(SelfOrMessageWithCondition(new int[] { 6, 3, 8, 4 }), Is.EqualTo("error!"));
}

To read more about list patterns, just go to Official documentation 🔗.

This article first appeared on Code4IT 🐧

finishing

This is a new feature in C#. Have you ever used it in your production code?

Or is it “just” nice functionality that no one uses? Message below if you have a real use if this 📩

I hope you enjoyed this article! Let’s keep in touch Twitter or on LinkedIn, If you want! 🤜🤛

Happy coding!

🐧

.

Source

Popular Articles