Deborah Kurata's Favorite 'New-ish' C# Feature: Pattern Matching

We often hear about the big new features in .NET or C#. But what about all of those lesser-known but useful new features like collection indices and ranges, date features, pattern matching and records?

C# coders who want to stay current with the state of their favorite programming language might have a hard time keeping up with its "new-ish" features as it rapidly evolves -- and likewise with .NET -- which is why we have Deborah Kurata.

The Microsoft MVP, Google Developer Expert (GDE), consultant, author and speaker will be explaining those features in a presentation at the five-day, in-person Visual Studio Live! developer conference taking place May 15-19 in Nashville. She will share her multi-discipline expertise in a session titled Useful New-ish Features in .NET/C#.

Attendees of the presentation will learn:

  • About some of the new-ish features in C#
  • About some of the new-ish features in .NET
  • How to leverage these features in your current projects

We caught up with the very busy Kurata to learn more about her presentation, in which she promises to discuss how new-ish features in C# minimize the amount of code a developer needs to write, and later read and maintain. These include top-level statements, global using directives, implicit usings and file-scoped namespaces. One preview feature in the upcoming C# 12, for example, is a primary constructor, which simplifies how devs build constructors for their classes.

However, one C# feature especially favored by Kurata is pattern matching, explained by Microsoft thusly:

Pattern matching is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "is expression" supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "switch expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of patterns.

She explained that pattern matching isn't exactly new, but is certainly -ish.

"Pattern matching was new in C# 7 and has been enhanced through many of the recent C# versions, including C# 10 and C# 11," Kurata told Visual Studio Magazine.

Inside the Session

What: Useful New-ish Features in .NET/C#

When: May 16, 8 a.m. - 9:15 a.m.

Who: Deborah Kurata, Microsoft MVP and Google Developer Expert (GDE) Consultant, Speaker, Author

Why: Learn why would want to use new-ish .NET and C# features to enhance and simplify your code.

Find out more about Visual Studio Live!, taking place May 15-19 in Nashville

She explained that when she first discovered pattern matching, she was a bit lost in the terms, pattern names and syntax, and so didn't think the feature would be useful in her day-to-day coding. But after spending some time diving into the feature's purpose, she realized she was missing a great opportunity to simplify her code.

"Most of our applications have lots of logic," she said. "We have business logic such as if the user has good credit and they're buying a new vehicle then ..., but if they have moderate credit and buying a used vehicle then ... and so on. We have validation logic such as if the model is a 3, the performance package is not allowed and only five passengers. And display logic such as if the user clicked a sort order and a filter then ..., but if the user clicked a sort order with no filter then ... and so on."

Pattern matching, she said, can dramatically simplify and clarify such "if" logic, making code easier to read, understand, modify and test. An added benefit is that the compiler will tell you if you're missing any cases, which is a huge benefit when dealing with complex logic.

"We can use pattern matching to match on Boolean values, string values, tuples, arrays, and object properties," Kurata said.

"This provides a tremendous amount of power and flexibility for implementing and simplifying our logic."

Deborah Kurata, Microsoft MVP and Google Developer Expert (GDE), Consultant, Speaker, Author

"This provides a tremendous amount of power and flexibility for implementing and simplifying our logic."

Furthermore, she said, "One of the newest pattern matching features is the ability to match an array or list against a sequence of patterns. A common scenario for using this pattern is for processing CSV files."

Along with pattern matching, Kurata will explain what many of the other new-ish features are, how to use them and, more importantly, why you would want to use them to enhance and simplify your code. Her 75-minute session starts at 8 a.m. on May 16, the second day of the conference.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus