Patterns are used to test whether a value matches a specific expectation and if it matches patterns allow to extract information from the value. You already create such pattern matchings by writing if and switch statements. With these statements you test values and if they match the expectation you extract and use the values information.
With C# 7 we got an extension to the syntax for is and case statements. This syntax extension allows combine the two steps: testing a value and extract its information.
Introduction
Let’s start with a basic example to see what we are talking about. The following source code shows how to test whether a value is of specific type and then use the value for a console output. The code shows the old and new syntax so you can compare these two implementations. As you can see the new syntax combines the value testing and information extraction in one short statement.
static void Main(string[] args) { WriteValueCS7("abc"); WriteValueCS6(15); WriteValueCS7(18.4); } static void WriteValueCS7(dynamic x) { //C# 7 if (x is int i) Console.WriteLine("integer: " + i); else if (x is string s) Console.WriteLine("string: " + s); else Console.WriteLine("not supported type"); } static void WriteValueCS6(dynamic x) { //C# 6 if (x is int) { var i = (int)x; Console.WriteLine("integer: " + i); } else if (x is string) { var s = x as string; Console.WriteLine("string: " + s); } else { Console.WriteLine("not supported type"); } }
The example shows pattern matching used in an is-expression to do a type check. The new pattern matching syntax is furthermore supported in case-expressions and it allows three different type of patterns: the type pattern, the const pattern and the var pattern. We will see these different possibilities within the next paragraphs.
Type Pattern
We have already seen the type pattern matching within the previous example. It is used to check whether a value is of a specific type. If the type is matching a new variable of this type is created and can be used to extract the value information. If a value is null, the type check always returns false. The following source code shows an according example.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; WriteValue(a); // output: 'string: abc' WriteValue(b); // output: 'not supported type' WriteValue(c); // output: 'integer: 15' } static void WriteValue(dynamic x) { if (x is int i) Console.WriteLine("integer: " + i); else if (x is string s) Console.WriteLine("string: " + s); else Console.WriteLine("not supported type"); }
Const Pattern
The pattern matching can be used to check whether the value matches a constant. Within this pattern you cannot create a new variable with the value information as the value already matches a constant and can be used as it is.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; int d = 17; WriteValue(a); // output: 'const: abc' WriteValue(b); // output: 'const: null' WriteValue(c); // output: 'const: 15' WriteValue(d); // output: 'unknown' } static void WriteValue(dynamic x) { if (x is 15) Console.WriteLine("const: 15"); else if (x is "abc") Console.WriteLine("const: abc"); else if (x is null) Console.WriteLine("const: null"); else Console.WriteLine("unknown"); }
Var Pattern
The var pattern is a special case of the type pattern with one major distinction: the pattern will match any value, even if the value is null. Following we see the example previously used for the type pattern, extended with the var pattern.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; WriteValue(a); // output: 'string: abc' WriteValue(b); // output: 'not supported type' WriteValue(c); // output: 'integer: 15' } static void WriteValue(dynamic x) { if (x is int i) Console.WriteLine("integer: " + i); else if (x is string s) Console.WriteLine("string: " + s); else if (x is var v) Console.WriteLine("not supported type"); }
If we look at this example we may ask two critical questions: Why do we have to specify a temporary variable for the var pattern if we dont use it? And why do we use the var pattern at all is it is the same as the empty (default) else-statement?
The first question is easy to answer. If we use the var pattern and don’t need the target variable we can use the discard wildcard „_“ which was also introduced with C# 7.
The second question is more difficult. As described, the var pattern always matches. So, it represents a default case, which is the empty else in an if-else statement. Therefore, if we just want to write the default else-case we should not use the var pattern at all. But the var pattern proves to be practical as we want to distinguish between different groups of default-cases. The following code shows an according example. It uses more than one var-pattern to handle the default-case in more detail. As mentioned above the last var pattern is unnecessary and you can write an empty else. I used the var pattern anyway to show you how to use the discard character.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; double d = 17.5; Guid e = Guid.NewGuid(); WriteValue(a); // output: 'string: abc' WriteValue(b); // output: ''null' is not supported' WriteValue(c); // output: 'integer: 15' WriteValue(d); // output: 'not supported primitive type' WriteValue(e); // output: 'not supported type' } static void WriteValue(dynamic x) { if (x is int i) Console.WriteLine("integer: " i); else if (x is string s) Console.WriteLine("string: " + s); else if ((x is var v) && (v == null)) Console.WriteLine("'null' is not supported"); else if ((x is var o) && (o.GetType().IsPrimitive)) Console.WriteLine("not supported primitive type"); else if (x is var _) Console.WriteLine("not supported type"); }
Switch-case
At the beginning of the article I mentioned that pattern matching can be used in if-statements and switch-statements. Now we know the three types of pattern matching and have used them in if-statements. At next we will see how to use the patterns in switch-statements.
The switch-statement so far was a pattern expression. it supported the const pattern only and was limited to numeric types and the string type. With C# 7 those restrictions have been removed. Now the switch-statement supports pattern matching and therefore all three patterns can be used. Furthermore, a variable of any type may be used in a switch statement.
The new possibilities have an side-effect which made it necessary to change the behavior of the switch-case-statement. So far, the switch statement supported const pattern only and therefore the case-clauses were unique. With the new pattern matching the case-clauses can overlap and may not be unique anymore. Therefore, the order of the case-clauses matters. For example, the compiler emits an error if the previous clause matches a base type and the next clause matches a derived type. Because of the possible overlapping case-clauses, each case must end with a break or return. This prevents code execution to „fall through“ from one case expression to the next.
The following example shows the type pattern used in an switch-case-statement.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; WriteValue(a); // output: 'string: abc' WriteValue(b); // output: 'not supported type' WriteValue(c); // output: 'integer: 15' } static void WriteValue(dynamic x) { switch (x) { case int i: Console.WriteLine("integer: " + i); break; case string s: Console.WriteLine("string: " + s); break; default: Console.WriteLine("not supported type"); break; } }
Switch-case with predicates
Another feature related to pattern matching is the ability to use predicates within the switch-case-statement. Within a case-clause a when-clause can be used to do more specific checks.
The following source code shows the use case we already seen in the var pattern example. But this time we use the switch-case and where statements instead of the if-statement.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; double d = 17.5; Guid e = Guid.NewGuid(); WriteValue(a); // output: 'string: abc' WriteValue(b); // output: ''null' is not supported' WriteValue(c); // output: 'integer: 15' WriteValue(d); // output: 'not supported primitive type' WriteValue(e); // output: 'not supported type' } static void WriteValue(dynamic x) { switch (x) { case int i: Console.WriteLine("integer: " + i); break; case string s: Console.WriteLine("string: " + s); break; case var v when v == null: Console.WriteLine("'null' is not supported"); break; case var o when o.GetType().IsPrimitive: Console.WriteLine("not supported primitive type"); break; default: Console.WriteLine("not supported type"); break; } }
Scope of pattern variables
A variable introduced within a type pattern or var pattern in an if-statement is lifted to the outer scope. This leads to strange behavior of the compiler. On the one hand it is not meaningful to use the variable outside the if-statement because it may not be initialized. And on the other hand, the compiler behavior is different for an if-statement and an else-if statement. But maybe this strange behavior will be fixed in a next compiler version. The following source code shows an according example with the compiler errors as comments.
static void Main(string[] args) { string a = "abc"; string b = null; int c = 15; WriteValue(a); // output: 'string: abc' WriteValue(b); // output: 'not supported type' WriteValue(c); // output: 'integer: 15' } static void WriteValue(dynamic x) { if (x is int i) Console.WriteLine("integer: " + i); else if (x is string s) Console.WriteLine("string: " + s); else Console.WriteLine("not supported type"); Console.WriteLine(i); // error: Use of unassigned local variable 'i' i = 15; // ok // s = "abc"; // error: The name 's' does not exist in the current context string s = "abc"; // error: 's' cannot be declared in this scope because that name is used in a local or parameter }
Pattern variables created inside a case-clause are only valid within the case-clause. They are not lifted outside the switch-case scope. In my opinion this leads to a clean separation of concerns and it would be nice to have the same behavior in if-statements.
Summary
Pattern matching is a powerful concept. The pattern matching possibilities introduced with C# 7 offer nice ways to write complex if-statements and switch-statements in a clean way. The patterns introduced so far are just some base ones and with C# 8 it is planned to add some more advanced ones like recursive pattern, positional pattern and property pattern. So, this programming concept is not just syntactical sugar, it will become an important concept in C# and introduces more and more functional programming techniques to the language.