filmov
tv
Pattern Matching in C# - Complete tutorial with examples - C# New Features

Показать описание
Pattern matching is a relatively new feature in C#. It was first introduced to the C# language in C# 7.0 and has since seen additional improvements in each succeeding version of C#. This video will explore the various use cases for pattern matching as well as how pattern matching has evolved over each version of C#.
00:00 - What is pattern matching?
Pattern matching is the process of taking an expression and testing whether it matches certain criteria, such as ‘being a specified type’ or ‘matching a specified constant value’. C# supports various different patterns which will be explored within this video.
01:57 - Constant pattern
The constant pattern can be used to test whether an expression is equal to a specified constant. A popular use-case for the constant pattern is null checking. This checks whether an object is null.
04:15 - Declaration pattern
The declaration pattern can be used to determine at run-time whether an expression is of a certain type. A variable declaration can optionally be included in the declaration pattern. If the test expression matches the specified type, then the expression will be cast to this type and then assigned to the variable.
07:35 - var pattern
The var pattern can be used to match any expression and then assign it to a new declared variable. It is different to other patterns in that it always matches. So the purpose of a var pattern is to assign an expression to a variable, rather than testing an expression for a pattern. The var pattern is useful if you want to store property values in a variable if other patterns are matching, or if you want to store intermediate values in a temporary variable during calculations.
10:38 - Property pattern
Pattern matching was further improved in C# 8.0 and one new addition was the property pattern. The property pattern can be used for checking and comparing values of properties. The property pattern tests whether an expression’s properties/fields match the values of specified properties/fields. Each corresponding property or field must match and the expression must not be null.
13:22 - Positional pattern
The positional pattern is useful when testing a type that can be deconstructed. Deconstruction is a process of unpacking types into parts and storing them into new variables (object deconstruction).
16:54 - Discard Pattern
Discards (_) can be used where any value is accepted at that position.
18:56 - Tuple Pattern
The tuple pattern can be used to pattern match multiple input values. The tuple pattern is a particular way of using the positional pattern, but the object we match on is not deconstructed as it is already a tuple.
22:52 - Relational pattern
The relational pattern can be used for comparisons by testing how a value compares to a constant using comparison operators.
24:46 - Type pattern
Like the declaration pattern, you can use a type pattern to determine at run-time whether an expression is of a certain type. With the type pattern, a variable is not specified.
27:35 - Switch Statement
The switch statement has been empowered to support pattern matching. The switch statement can be used when branching code by testing an expression against a set of patterns.
30:37 - Switch expression
C# 8.0 introduced the switch expression to evaluate an expression against a set of patterns. The switch expression provides a more concise syntax for pattern matching. There are several syntax improvements to the switch expression as compared to the switch statement.
33:44 - Summary
Pattern matching is a very powerful new feature in C# which can be utilised to make your code more concise and readable.
00:00 - What is pattern matching?
Pattern matching is the process of taking an expression and testing whether it matches certain criteria, such as ‘being a specified type’ or ‘matching a specified constant value’. C# supports various different patterns which will be explored within this video.
01:57 - Constant pattern
The constant pattern can be used to test whether an expression is equal to a specified constant. A popular use-case for the constant pattern is null checking. This checks whether an object is null.
04:15 - Declaration pattern
The declaration pattern can be used to determine at run-time whether an expression is of a certain type. A variable declaration can optionally be included in the declaration pattern. If the test expression matches the specified type, then the expression will be cast to this type and then assigned to the variable.
07:35 - var pattern
The var pattern can be used to match any expression and then assign it to a new declared variable. It is different to other patterns in that it always matches. So the purpose of a var pattern is to assign an expression to a variable, rather than testing an expression for a pattern. The var pattern is useful if you want to store property values in a variable if other patterns are matching, or if you want to store intermediate values in a temporary variable during calculations.
10:38 - Property pattern
Pattern matching was further improved in C# 8.0 and one new addition was the property pattern. The property pattern can be used for checking and comparing values of properties. The property pattern tests whether an expression’s properties/fields match the values of specified properties/fields. Each corresponding property or field must match and the expression must not be null.
13:22 - Positional pattern
The positional pattern is useful when testing a type that can be deconstructed. Deconstruction is a process of unpacking types into parts and storing them into new variables (object deconstruction).
16:54 - Discard Pattern
Discards (_) can be used where any value is accepted at that position.
18:56 - Tuple Pattern
The tuple pattern can be used to pattern match multiple input values. The tuple pattern is a particular way of using the positional pattern, but the object we match on is not deconstructed as it is already a tuple.
22:52 - Relational pattern
The relational pattern can be used for comparisons by testing how a value compares to a constant using comparison operators.
24:46 - Type pattern
Like the declaration pattern, you can use a type pattern to determine at run-time whether an expression is of a certain type. With the type pattern, a variable is not specified.
27:35 - Switch Statement
The switch statement has been empowered to support pattern matching. The switch statement can be used when branching code by testing an expression against a set of patterns.
30:37 - Switch expression
C# 8.0 introduced the switch expression to evaluate an expression against a set of patterns. The switch expression provides a more concise syntax for pattern matching. There are several syntax improvements to the switch expression as compared to the switch statement.
33:44 - Summary
Pattern matching is a very powerful new feature in C# which can be utilised to make your code more concise and readable.