in Technology by
What is Pattern matching enhancements in C#.Net?

1 Answer

0 votes
by

C# 9 includes new pattern matching improvements:

  • Type patterns match a variable is a type
  • Parenthesized patterns enforce or emphasize the precedence of pattern combinations
  • Conjunctive and patterns require both patterns to match
  • Disjunctive or patterns require either pattern to match
  • Negated not patterns require that a pattern doesn’t match
  • Relational patterns require the input be less than, greater than, less than or equal, or greater than or equal to a given constant.

These patterns enrich the syntax for patterns. Consider these examples:

public static bool IsLetter(this char c) =>
    c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';

Alternatively, with optional parentheses to make it clear that and has higher precedence than or:

public static bool IsLetterOrSeparator(this char c) =>
    c is (>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or '.' or ',';

One of the most common uses is a new syntax for a null check:

if (e is not null) { // ... }

Any of these patterns can be used in any context where patterns are allowed: is pattern expressions, switch expressions, nested patterns, and the pattern of a switch statement’s case label.

Related questions

0 votes
    What is Pattern Matching in SQL?...
asked Dec 11, 2020 in Technology by JackTerrance
0 votes
    This is kind of hard to explain, I hope my English is sufficient: I have a class "A" which ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    This is kind of hard to explain, I hope my English is sufficient: I have a class "A" which ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    What do you understand by the record matching and data lookup features in ServiceNow?...
asked May 24, 2021 in Technology by JackTerrance
0 votes
    What is the minimal set of information for matching dependency reference against a dependencyManagement section?...
asked May 4, 2021 in Technology by Editorial Staff
0 votes
    What does the matching score at first layer in recognition hamming network is indicative of? (a) dissimilarity of ... ) none of the mentioned Please answer the above question....
asked Aug 26, 2022 in Education by JackTerrance
0 votes
    I am looking to get row count to check if same email is already in database or not. i have tried ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Here's a puzzle... I have two databases of the same 50000+ electronic products and I want to match products ... I tackle this problem? Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    I have two data frames with the closest matching DateTime index, sometimes matching. An object is to merge two of ... I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    What can be used to locate messages matching certain criteria Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    Which join refers to join records from the right table that have no matching key in the left table are ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...